ObservableCollection< T> CollectionChanged事件 [英] ObservableCollection<T> CollectionChanged event

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

问题描述

我有一个观察的集合,我有一个collectionChanged事件接线上。
我有它的项目绑定到用户界面列表框。
当用户删除从列表框中UI的一些项目中,CollectioChanged得到妥善解雇,但是,我需要知道被删除的项目的索引。
的问题是我不能够在集合中的indexOf在更改以后,因为它没有被删除的项目了..



我们可以得到?访问从collectionchanged事件一个ObservableCollection中删除的索引列表


解决方案

CollectionChanged 事件使用,让你一个的 NotifyCollectionChangedEventArgs 。这有一个 OldStartingIndex 属性,它会告诉你这是在删除索引。例如:

 无效美孚()
{
&的ObservableCollection LT;字符串> R =新的ObservableCollection<串GT;();
r.CollectionChanged + = r_CollectionChanged;
}

静态无效r_CollectionChanged(对象发件人,System.Collections.Specialized.NotifyCollectionChangedEventArgs E)
{
VAR itemRemovedAtIndex = e.OldStartingIndex;
}




假设我去除多个项目在收集使用不同oldStartingIndex将indices..So只要给我已删除




本次活动将最有可能起火的第一项指标多次,一旦为每个项目


I have an observable collection and I have a collectionChanged event wired up on that. I have its items bound to a listbox in the UI. When the user removes some items in the UI from the listbox, the CollectioChanged gets fired properly, however, I need to know the indices of the items that were removed. The problem is I am not able to an indexOf on the collection after it is changed because it does not have the removed item anymore..

Can we get access to the list of indexes that were removed from an ObservableCollection from the collectionchanged event?

解决方案

The CollectionChanged event uses an event that gives you a NotifyCollectionChangedEventArgs. This has a OldStartingIndex property, which will tell you the index it was removed at. For example:

void Foo()
{
    ObservableCollection<string> r = new ObservableCollection<string>();
    r.CollectionChanged += r_CollectionChanged;
}

static void r_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    var itemRemovedAtIndex = e.OldStartingIndex;
}

Assume that I am removing MULTIPLE items from the collection at different indices..So using the oldStartingIndex would just give me the first item index that was removed

The event will most likely fire multiple times, once for each item.

这篇关于ObservableCollection&lt; T&gt; CollectionChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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