
[英]WPF MVVM: How to reflect changes of ObservableCollection in the UI
[英]How to get changes in ObservableCollection
public ObservableCollection<IndividualEntityCsidClidDetail> IncludedMembers { get; set; }
假设我有一个IncludedMembers
的引用我希望在添加/删除/编辑集合项时发生事件。
处理CollectionChanged
事件
//注册事件,以便每次收集时都会调用CollectionChangedMethod
方法
yourCollection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler
(CollectionChangedMethod);
创建一个这样的方法
private void CollectionChangedMethod(object sender, NotifyCollectionChangedEventArgs e)
{
//different kind of changes that may have occurred in collection
if(e.Action == NotifyCollectionChangedAction.Add)
{
//your code
}
if (e.Action == NotifyCollectionChangedAction.Replace)
{
//your code
}
if (e.Action == NotifyCollectionChangedAction.Remove)
{
//your code
}
if (e.Action == NotifyCollectionChangedAction.Move)
{
//your code
}
}
只需注册该系列的CollectionChanged
活动即可。 它会在您添加或删除项目时引发事件,否则会更改集合的内容。
如果要在集合中项目的属性发生更改时接收事件,则需要确保项目首先是IObservable
然后IObservable
Subscribe()
到单个对象。
这就是可观察收藏品的用途。
只需绑定到集合,您就可以进行排序!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.