简体   繁体   English

WPF Listview不显示列表

[英]WPF Listview not displaying list

I have a wpf MVVM project that I am working on, I am using prism so INotifyPropertyChanged = SetProperty. 我有一个正在工作的wpf MVVM项目,我使用的是棱镜,所以INotifyPropertyChanged = SetProperty。 I have a model that is ComplaintModel: 我有一个模型是ComplaintModel:

public class ComplaintModel:BindableBase, IDataErrorInfo
{
    private Person _complaintPerson;
    private List<EmployeeModel> _crewList;

    public string MyStreet { get; set; }
    public string Complaint { get; set; }
    public string Response { get; set; }
    public DateTime? DateOfResponse { get; set; }
    public DateTime EntryDate { get; set; }
    public bool? Callout { get; set; }
    public string Quad { get; set; }
    public string Tap { get; set; }
    public DateTime? Modified { get; set; }

    public Person ComplaintPerson
    {
        get { return _complaintPerson; }
        set { SetProperty(ref (_complaintPerson), value); }
    }

    public List<EmployeeModel> CrewList
    {
        get { return _crewList; }
        set { SetProperty(ref (_crewList), value); }
    }

    public int ComplaintID { get; set; }

and the property in the ViewModel: 以及ViewModel中的属性:

private ComplaintModel _selectedComplaint;
public ComplaintModel SelectedComplaint
{
    get { return _selectedComplaint; }
    set { SetProperty(ref (_selectedComplaint), value); }
}

The EmployeeModel is EmployeeModel是

public class EmployeeModel:BindableBase,IDataErrorInfo
{
    private string _phone;
    private string _firstName;
    private string _lastName;
    public int Id { get; set; }

    public string FirstName
    {
        get { return _firstName; }
        set { SetProperty(ref(_firstName),value); }
    }

    public string LastName
    {
        get { return _lastName; }
        set { SetProperty(ref(_lastName),value); }
    }

    public string Phone
    {
        get { return _phone; }
        set { SetProperty(ref (_phone), value); }
    }

So I try to addEmployee: 所以我尝试添加员工:

private void AddEmployee()
{

    if (SelectedEmployee == null)
    {
        SetMessage(Messages.ChooseEmployeeMessage);
        return;
    }
    if (IsReadOnly)
    {
        SetMessage(Messages.MakeEditableMessage);
        return;
    }
    if (SelectedComplaint.CrewList == null)
    {
        SelectedComplaint.CrewList = new List<EmployeeModel>();
    }

    var myList = SelectedComplaint.CrewList;
    var employee = new EmployeeModel() {FirstName = "James", Id = 1, LastName = "Tays", Phone = "1234567"};
    myList.Add(employee);
    employee = new EmployeeModel() { FirstName = "John", Id = 2, LastName = "Doe", Phone = "1234567" };
    myList.Add(employee);
    SelectedComplaint.CrewList = myList;
    SelectedEmployee = null;
}

This adds the two employees to the CrewList but doesn't update the view.The funny thing is when I change var myList = SelectedComplaint.CrewList to var myList = new List<EmployeeModel>(); 这将两个雇员添加到CrewList,但不更新视图。有趣的是,当我将var myList = SelectedComplaint.CrewList更改为var myList = new List<EmployeeModel>(); it will add the 2 employees and update the view. 它将添加2名员工并更新视图。 I have also tried the SelectedComplaint.CrewList.Add(employee) and this didnt update the view. 我也尝试了SelectedComplaint.CrewList.Add(employee) ,但这并没有更新视图。

please let me know if you see an area that I have missed. 如果您看到我错过的地方,请告诉我。

尝试将CrewList类型声明为“ observablecollection”而不是“ List”。添加每个员工时,它将更新视图。

ObservableCollection<EmployeeModel> CrewList

Calling Add will not be treated as changing the property. 调用添加将不会被视为更改属性。 Here's why: When you do: 原因如下:执行操作时:

    var myList = SelectedComplaint.CrewList;
    var employee = new EmployeeModel() {FirstName = "James", Id = 1, LastName = "Tays", Phone = "1234567"};
    myList.Add(employee);
    employee = new EmployeeModel() { FirstName = "John", Id = 2, LastName = "Doe", Phone = "1234567" };
    myList.Add(employee);
    SelectedComplaint.CrewList = myList;

You: 您:

1) Call the getter of the CrewList . 1)调用CrewList This return a REFERENCE to _crewList object 这将返回对_crewList对象的引用

2) You Add the Employee - this modifies the list, but this doesn't call the setter of the CrewList - thus, view is not notified of the modified list 2)您AddEmployee这会修改列表,但这不会调用CrewList因此,不会向视图通知已修改的列表

3) You set the CrewList property to the value of the myList variable. 3)您将CrewList属性设置为myList变量的值。 But, notice what I said in point 1 - myList is a REFERENCE to the same object - this will not change your property (probably Prism is not notifying the view if the object is not changed in the setter) 但是,请注意我在第1点中所说的myList是对同一对象的引用-这不会更改您的属性(如果在setter中未更改对象,则Prism不会通知视图)

My suggestion is to change the List to be an ObservableCollection - this class has an additional "event" called NotifyCollectionChanged - which is automatically raised by the ObservableCollection when you call Add() , Remove() etc. This will notify the View for you. 我的建议是将List更改为ObservableCollection此类具有一个名为NotifyCollectionChanged的附加“事件”-当您调用Add()Remove()等时, ObservableCollection会自动引发该事件。这将为您通知View。

You need to change 你需要改变

 public List<EmployeeModel> CrewList
{
    get { return _crewList; }
    set { SetProperty(ref (_crewList), value); }
}

to

public ObservableCollection<EmployeeModel> CrewList {get;set}

This will automatically call the NotifyCollectionChange event on the collection. 这将自动在集合上调用NotifyCollectionChange事件。

This post contains more information on the difference between list and observable collections. 这篇文章包含有关列表集合和可观察集合之间差异的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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