简体   繁体   中英

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;

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.

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.

By default your member variables are going to be private. If what you have posted here is your actual code, then the XAML has no way of accessing it. Makes those properties public:

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

1.Your list needs to be a public property (Edit: unless you set the DataContext like you did...) ( public ObservableCollection<Project> Projects {get;} )

2.Your notification should be the same as the actual property name:

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

3.The binding needs to be that way as well:

<TextBlock Text="{Binding Path=ID}"/>

I think...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM