c# - WPF将项目添加到BindingList不会更新Viewmodel中的另一个属性

标签 c# wpf mvvm bindinglist

我的 View 模型(单例)中有这段代码:

    /// <summary>
    /// Gets or sets the person list.
    /// </summary>
    /// <value>
    /// The person list.
    /// </value>
    public BindingList<Person> PersonList
    {
        get { return personList; }
        set
        {
            personList = value;
            OnPropertyChanged(nameof(PersonList));
            OnPropertyChanged(nameof(PersonCount));
        }
    }

    /// <summary>
    /// Gets the person count.
    /// </summary>
    /// <value>
    /// The person count.
    /// </value>
    public int PersonCount
    {
        get
        {
            return personList.Count();
        }
    }

当我将 Person 添加到 BindingList 时,它会正确显示在 View 中。正确显示了 BindingList 的一项更改。
但 PersonCount 未更新并显示为“0”。

Person 类的实现如下:
public class Person : NotificationObject
{
    #region Fields

    private string name;
    private DateTime birthDate;
    private string lastName;

    #endregion // Fields

    #region Construction
    #endregion // Construction

    #region Attributes

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            OnPropertyChanged(nameof(Name));
        }
    }

    public string LastName
    {
        get { return lastName; }
        set
        {
            lastName = value;
            OnPropertyChanged(nameof(LastName));
        }
    }

    public DateTime BirthDate
    {
        get { return birthDate; }
        set
        {
            birthDate = value;
            OnPropertyChanged(nameof(BirthDate));
            OnPropertyChanged(nameof(Age));
        }
    }

    public int Age
    {
        get { return (DateTime.Now - birthDate).Days/365; }
    }

    #endregion // Attributes

    #region Operations
    #endregion // Operations

    #region Implementation
    #endregion // Implementation
} // class Person

为什么 PersonCount 没有更新以及如何正确执行?
我做错了什么?

提前致谢,
安德烈亚斯

最佳答案

您的代码中的问题是,一旦将“PersonList”绑定(bind)到 View 模型,列表对象就不会改变,除非您绑定(bind)另一个列表(发生的只是从列表中添加或删除项目),因此 setter 未调用“PersonList”属性,因此“通知属性已更改”不会为“PersonCount”触发。

但是,由于“PersonList”是 绑定(bind)列表 ,添加/删除的项目反射(reflect)在 UI 中。

解决方案

1) 将“PersonList”注册到列表已更改 事件并在事件处理程序中调用“通知属性已更改”。

    public BindingList<Person> PersonList
    {
        get { return personList; }
        set
        {
            if (personList != null)
                personList.ListChanged -= PersonList_ListChanged;

            personList = value;

            if (personList != null)
                personList.ListChanged += PersonList_ListChanged;

            OnPropertyChanged(nameof(PersonList));
        }
    }

    public PersonsDashboardViewModel()
    {
        PersonList = new BindingList<Person>();

        PersonList.Add(new Person("Name1", "Lname1"));
        PersonList.Add(new Person("Name2", "Lname2"));
    }

    private void PersonList_ListChanged(object sender, ListChangedEventArgs e)
    {
        OnPropertyChanged(nameof(PersonCount));
    }



2) 为添加人员项目的“PersonCount”调用“通知属性已更改”。
    public void AddPersonItem()
    {
        PersonList.Add(new Person("Sean", "Brendon"));
        OnPropertyChanged(nameof(PersonCount));
    }

关于c# - WPF将项目添加到BindingList不会更新Viewmodel中的另一个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53445315/

相关文章:

c# - 在 WPF C# 应用程序中将 XPS 转换为 PDF

c# - 使用 user32.dll 获取特定窗口的句柄

c# - 从 C# 调用 C++ 代码

c# - 我应该通过绑定(bind)使用 MVP 和 WPF 显示数据吗?

时间:2019-03-07 标签:c#

c# - 从一幅图像淡出并淡入另一幅图像

c# - 如何在 wpf 中检测拖动开始

c# - MVVM 命令绑定(bind)

c# - 如何创建 ItemCollection 的新实例

c# - 带有更新 2 的 Visual Studio 2015 - 'The Scc Display Information package did not load correctly'