CollectionChanged 样本 [英] CollectionChanged sample

查看:29
本文介绍了CollectionChanged 样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指出实现 CollectionChanged 的​​示例吗?我正在使用 wpf mvvm 灯.我试着用谷歌搜索,没有找到足够好的东西.

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 样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆