public ObservableCollection<IndividualEntityCsidClidDetail> IncludedMembers { get; set; }
假设我有对IncludedMembers
的引用,当集合项被添加/删除/编辑时,我希望发生事件。
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
}
}
NotifyCollectionChangedAction.Reset
呢?它是否也会调用NotifyCollectionChangedAction.Remove
呢? - Deantwo只需注册到集合的CollectionChanged
事件即可。当您添加或删除项目或以其他方式更改集合的内容时,它将引发事件。
如果您想在集合中的项目属性更改时接收事件,则需要确保项目首先是IObservable
,然后Subscribe()
到各个对象。
这就是可观察集合的作用。
只需要将其绑定到集合上,你就完成了!