什么是notifycollectionchangedaction重置值

发布于 2024-10-08 17:41:45 字数 325 浏览 6 评论 0原文

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

  1. Private T item。
  2. Private bool 被选中。

当 IsSelected 属性更改时...我的集合的更改属性不会触发。

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

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

  1. Private T item.
  2. Private bool isSelected.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

土豪我们做朋友吧 2024-10-15 17:41:45

这是一个老问题,但为了任何可能像我一样通过搜索遇到这个问题的人的利益:

NotifyCollectionChangedAction.Reset 意味着“集合的内容发生了巨大的变化”。引发 Reset 事件的一种情况是当您对底层可观察集合调用 Clear() 时。

通过 Reset 事件,您不会在 NotifyCollectionChangedEventArgs 参数中获取 NewItemsOldItems 集合。

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

一个例子可能是这样的:

((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 中没有项目

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

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.

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

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

终陌 2024-10-15 17:41:45

INotifyCollectionChangedINotifyPropertyChanged 之间存在差异。

当对象中的属性值发生更改时,应该使用 INotifyPropertyChanged 接口实现通知其他人。

另一方面,当集合中的项目数量项目本身发生变化时,应该使用INotifyCollectionChanged实现让其他人知道。

现在,在您的情况下,集合中对象的属性值发生变化。这应该引发 PropertyChanged 事件,而不是 CollectionChanged 事件。

There is a difference between INotifyCollectionChanged and INotifyPropertyChanged.

When a value of a propery in an object changes, it should notify others using INotifyPropertyChanged interface implementation.

On the other hand, when number of items or items themselves change in a collection, it should let others know using INotifyCollectionChanged implementation.

Now, in your case, value of a property of an object in your collection changes. That is supposed to raise PropertyChanged event, not CollectionChanged event.

坠似风落 2024-10-15 17:41:45

当且仅当您通过添加新项目或从集合中删除现有项目来修改集合时,才会触发集合更改。

Collection changed will be fired if and only if you modify the collection that is via either Adding a new Item or Removing an existing item from the collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文