什么是notifycollectionchangedaction重置值 [英] what is notifycollectionchangedaction reset value

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

问题描述

我有一个可观察的集合... SelectableDataContext<T> ..并且在通用类SelectableDataContext<T>中...具有两个私有成员变量

I have an observable collection...SelectableDataContext<T>..And in the generic class SelectableDataContext<T> is...having two private member variables

  1. 私人T物品.
  2. 已选择私人布尔.

当IsSelected属性更改时...我的收藏集的更改属性未触发.

When the IsSelected property changes...My collection's changed property is not firing .

我认为它应该触发...因为它是INotifyCollectionChangedAction中的Reset.

I think it should fire...because it's Reset in INotifyCollectionChangedAction.

推荐答案

这是一个古老的问题,但是对于任何可能像我一样通过搜索遇到此问题的人来说都是如此:

This is an old question but for the benefit of anyone who may come across this through a search as I did:

NotifyCollectionChangedAction.Reset的意思是集合的内容发生了巨大变化".引发Reset事件的一种情况是在基础可观察集合上调用Clear().

NotifyCollectionChangedAction.Reset means "The content of the collection changed dramatically". One case where the Reset event is raised is when you call Clear() on the underlying observable collection.

使用Reset事件,您不会在NotifyCollectionChangedEventArgs参数中获得NewItemsOldItems集合.

With the Reset event, you don't get the NewItems and OldItems collections in the NotifyCollectionChangedEventArgs parameter.

这意味着您最好使用事件的发送者"来获取对修改后的集合的引用,并直接使用该引用,即假定它是一个新列表.

This means you're better off using the "sender" of the event to get a reference to the modified collection and use that directly, i.e. assume it's a new list.

例如:

((INotifyCollectionChanged)stringCollection).CollectionChanged += new NotifyCollectionChangedEventHandler(StringCollection_CollectionChanged);
  ...

void StringCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    switch (e.Action)
    {
        case NotifyCollectionChangedAction.Add:
            foreach (string s in e.NewItems)
            {
                InternalAdd(s);
            }
            break;

        case NotifyCollectionChangedAction.Remove:
            foreach (string s in e.OldItems)
            {
                InternalRemove(s);
            }
            break;

        case NotifyCollectionChangedAction.Reset:
            ReadOnlyObservableCollection<string> col = sender as ReadOnlyObservableCollection<string>;
            InternalClearAll();
            if (col != null)
            {
                foreach (string s in col)
                {
                    InternalAdd(s);
                }
            }
            break;
    }
}

此处有关此重置事件的讨论很多:

Lots of discussions on this Reset event here: When Clearing an ObservableCollection, There are No Items in e.OldItems.

这篇关于什么是notifycollectionchangedaction重置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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