本文介绍了CollectionChanged示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以指出实现CollectionChanged的示例吗?我正在使用wpf mvvm light.我尝试去Google搜寻,找不到足够好的东西.
Can someone point to an example where CollectionChanged is implemented. I am using wpf mvvm light. I tried to google, didn't find anything good enough.
推荐答案
public ObservableCollection<string> Names { get; set; }
public ViewModel()
{
names = new ObservableCollection<string>();
Names.CollectionChanged += this.OnCollectionChanged;
}
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
//Get the sender observable collection
ObservableCollection<string> obsSender = sender as ObservableCollection<string>;
List<string> editedOrRemovedItems = new List<string>();
foreach(string newItem in e.NewItems)
{
editedOrRemovedItems.Add(newItem);
}
foreach(string oldItem in e.OldItems)
{
editedOrRemovedItems.Add(oldItem);
}
//Get the action which raised the collection changed event
NotifyCollectionChangedAction action = e.Action;
}
有关NotifyCollectionChangedEventArgs的更多信息,请参见此处.
For more information about the NotifyCollectionChangedEventArgs look here.
因为您需要添加/删除项目的列表,所以我修改了示例代码.
Because you need a list of added/removed items, I modified the sample code.
这篇关于CollectionChanged示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!