C# ObservableCollection OnCollectionChanged 在项目更改时不会触发

发布于 2024-11-30 06:21:50 字数 909 浏览 0 评论 0原文

来自 MSDN 关于 OnCollectionChanged 的​​内容:“在添加、删除、更改、移动项目或刷新整个列表时发生。”

我正在更改附加到位于我的集合中的 obj 的属性,但 OnCollectionChanged 未触发。我正在 obj 类上实现 iNotifyPropertyChanged。

public class ObservableBatchCollection : ObservableCollection<BatchData>
    {
        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if(e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                foreach (BatchData item in e.NewItems)
                {

                }
            }
            base.OnCollectionChanged(e);
        }

        public ObservableBatchCollection(IEnumerable<BatchData> items)
            : base(items)
        {

        }
}

对我来说,这意味着当集合中的某个项目发生更改(例如对象的属性)时,应该触发此事件。然而事实并非如此。我希望能够知道自定义集合中的项目何时发生变化,以便在需要时对其执行计算。

有什么想法吗?

From the MSDN about OnCollectionChanged: "Occurs when an item is added, removed, changed, moved, or the entire list is refreshed."

I'm changing a property attached to an obj that resides in my collection, but OnCollectionChanged isn't fired. I am implementing iNotifyPropertyChanged on the obj class.

public class ObservableBatchCollection : ObservableCollection<BatchData>
    {
        protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if(e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                foreach (BatchData item in e.NewItems)
                {

                }
            }
            base.OnCollectionChanged(e);
        }

        public ObservableBatchCollection(IEnumerable<BatchData> items)
            : base(items)
        {

        }
}

To me, that reads that when an item in the collection is changed, such as a property of the object, that this event should fire. It's not, however. I want to be able to know when an item in my custom collection changes so I can perform a calculation on it, if needed.

Any thoughts?

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

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

发布评论

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

评论(1

烂柯人 2024-12-07 06:21:50

ObservableCollection 仅当集合本身发生更改时才会引发事件。集合中包含的内部状态发生变化的项目并未改变集合的结构,并且 ObservableCollection 不会报告它。

一种选择是子类化 ObservableCollection 并在添加每个项目时订阅其 OnPropertyChanged 事件。在该处理程序中,您可以引发自定义事件,也可以回退到集合自己的 PropertyChanged 事件。请注意,如果您确实采用此路线,则应添加通用约束,以便 T : INotifyPropertyChanged

ObservableCollection<T> raises events only when the collection itself changes. An item contained in the collection that has its internal state mutated has not altered the structure of the collection, and ObservableCollection<T> will not report it.

One option is to subclass ObservableCollection<T> and subscribe to each item's OnPropertyChanged event when it is added. In that handler, you can raise either a custom event, or fall back to the collection's own PropertyChanged event. Note that if you do go this route, you should add a generic constraint so that T : INotifyPropertyChanged.

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