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

懵懂的女人 提交于 2020-01-14 08:20:13

问题


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;

回答1:


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.




回答2:


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.




回答3:


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>();



回答4:


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...



来源:https://stackoverflow.com/questions/4680653/getting-a-wpf-listview-to-display-observablecollectiont-using-databinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!