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

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

问题描述

我有一个可观察的集合...SelectableDataContext..并且在泛型类 SelectableDataContext 中...有两个私有成员变量

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 表示集合的内容发生了巨大变化".引发重置事件的一种情况是当您在底层 observable 集合上调用 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.

一个例子可能是这样的:

An example of this might be something like:

((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;
    }
}

这里有很多关于这个重置事件的讨论:清除 ObservableCollection 时,e.OldItems 中没有项目.

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

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

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