WPF 将项目添加到 BindingList 不会更新 Viewmodel 中的另一个 属性
WPF Adding an item to a BindingList not update another Property in Viewmodel
我的视图模型(单例)中有这段代码:
/// <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 时,它会正确显示在视图中。正确显示 BindingList 的其中一项的更改。
但是 PersonCount 没有更新并显示为“0”。
class人是这样实现的:
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 没有更新,如何正确更新?
我做错了什么?
提前致谢,
安德烈亚斯
您必须在修改列表的地方调用 OnPropertyChanged(nameof(PersonCount));
。如果不可能,那么您必须在 setter 中为 PersonList
订阅 CollectionChanged
并在事件处理程序中调用 OnPropertyChanged(nameof(PersonCount));
。
您的代码中的问题是,一旦您将 'PersonList' 绑定到视图模型,列表对象就不会更改,除非您绑定另一个列表(所发生的只是项目被添加或从列表),因此 'PersonList' 属性 的 setter 不会被调用,因此 "Notify property changed" 不会触发 'PersonCount'。
然而,由于 'PersonList' 是一个 BindingList,added/removed 项目反映在 UI。
解决方案
1) 将'PersonList'注册到ListChanged事件,并在事件处理程序中调用'Notify Property Changed'。
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' 调用 'Notify Property Changed' 添加人员项目。
public void AddPersonItem()
{
PersonList.Add(new Person("Sean", "Brendon"));
OnPropertyChanged(nameof(PersonCount));
}
我的视图模型(单例)中有这段代码:
/// <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 时,它会正确显示在视图中。正确显示 BindingList 的其中一项的更改。 但是 PersonCount 没有更新并显示为“0”。
class人是这样实现的:
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 没有更新,如何正确更新? 我做错了什么?
提前致谢, 安德烈亚斯
您必须在修改列表的地方调用 OnPropertyChanged(nameof(PersonCount));
。如果不可能,那么您必须在 setter 中为 PersonList
订阅 CollectionChanged
并在事件处理程序中调用 OnPropertyChanged(nameof(PersonCount));
。
您的代码中的问题是,一旦您将 'PersonList' 绑定到视图模型,列表对象就不会更改,除非您绑定另一个列表(所发生的只是项目被添加或从列表),因此 'PersonList' 属性 的 setter 不会被调用,因此 "Notify property changed" 不会触发 'PersonCount'。
然而,由于 'PersonList' 是一个 BindingList,added/removed 项目反映在 UI。
解决方案
1) 将'PersonList'注册到ListChanged事件,并在事件处理程序中调用'Notify Property Changed'。
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' 调用 'Notify Property Changed' 添加人员项目。
public void AddPersonItem()
{
PersonList.Add(new Person("Sean", "Brendon"));
OnPropertyChanged(nameof(PersonCount));
}