Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a WPF Listview to display ObservableCollection<T> using databinding

I have a observable collection of type Project, that I want to be displayed in a ListView but nothing is added to my ListView which I really dont understand

My MainWindow.xaml

            <ListView Name="ListViewProjects" Grid.Column="0" Grid.RowSpan="3" SelectionChanged="ListViewProjectsSelectionChanged" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" MinWidth="100">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Path=ProjectID}"/>
                        <TextBlock Text="{Binding Path=ProjectName}"/>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

My MainWindow.cs

    public partial class MainWindow : Window
{
    ObservableCollection<Project> Projects = new ObservableCollection<Project>();
    ObservableCollection<Employee> Employees = new ObservableCollection<Employee>();

    public MainWindow()
    {
        InitializeComponent();

        DataContext = Projects;

        Project pro1 = new Project(1, "Swordfish");
        Projects.Add(pro1);
        Employee empMads = new Employee("Mads", 1);
        Employee empBrian = new Employee("Brian", 2);
        Employees.Add(empMads);
        Employees.Add(empBrian);
    }

    private void ListViewProjectsSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    }
}

and my Project.cs which is the class file

[Serializable]
class Project : INotifyPropertyChanged
{
    public Project(int id, string name)
    {
        ID = id;
        Name = name;
    }

    private int id;
    public int ID
    {
        get { return id; }
        set
        {
            id = value;
            NotifyPropertyChanged("ProjectID");
        }
    }

    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            NotifyPropertyChanged("ProjectName");
        }
    }

    [field: NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

But nothing is added to my list I cant see what I am missing for it to work.

I have it as a observablecollection I do DataContext = the collection And I do binding in the xaml file

Edit codepart 1:

        public ObservableCollection<Project> Projects { get; set; }
    public ObservableCollection<Employee> Employees { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        Projects = new ObservableCollection<Project>();
        Employees = new ObservableCollection<Employee>();

        DataContext = Projects;
like image 668
Mech0z Avatar asked Jan 13 '11 13:01

Mech0z


People also ask

How do I bind ObservableCollection to listview?

The ObservableCollection is already bound to the Listview. So all we need to do in the XAML file is to specify the binding member for each column. We can do that by the "DisplayMemberBinding" attribute and "Binding" markup extension.

What is the difference between ObservableCollection and list?

One more important difference is you can access ObservableCollection only from thread on which it was created where as list can be accessed fromany thread. Save this answer. Show activity on this post. I see no problem with that, other than a very marginal performance overhead.

What is ObservableCollection in C#?

This interface exposes the CollectionChanged event, an event that should be raised whenever the underlying collection changes. WPF provides the ObservableCollection<T> class, which is a built-in implementation of a data collection that implements the INotifyCollectionChanged interface.


2 Answers

It's because you use binding path like ProjectID while your Project class has property ID. The same goes for ProjectName and Name properties.

E: When you have problems with binding it's very useful to look through Output tab in visual studio's debug mode to see what errors were returned from databinding engine. Those exceptions are normally not returned to user, but you can inspect them there.

like image 149
Jarek Avatar answered Nov 06 '22 10:11

Jarek


As pointed out, there are a few problems here. One of the "gotchas" that get you as a new WPF developer is that you can't use member variables, you have to use public properties:

public ObservableCollection<Project> Projects { get; set; }
public ObservableCollection<Employee> Employees { get; set; }

These, of course, need to be initialized in your constructor.

Once you do that, you need to make sure the properties of the collection match what you're binding to on the ListView.

like image 27
Robaticus Avatar answered Nov 06 '22 09:11

Robaticus