简体   繁体   中英

WPF Listview not displaying list

I have a wpf MVVM project that I am working on, I am using prism so INotifyPropertyChanged = SetProperty. I have a model that is 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:

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

The EmployeeModel is

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>(); it will add the 2 employees and update the view. I have also tried the SelectedComplaint.CrewList.Add(employee) and this didnt update the view.

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 . This return a REFERENCE to _crewList object

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

3) You set the CrewList property to the value of the myList variable. 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)

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.

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.

This post contains more information on the difference between list and observable collections.

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