当item的属性发生更改时,在ObservableCollection上引发PropertyChanged [英] Raise PropertyChanged on ObservableCollection when item's property is changed

查看:104
本文介绍了当item的属性发生更改时,在ObservableCollection上引发PropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我试图让我的ObservableCollection在其中的一个项目发生变化时提升它的属性改变事件。 />


我在下面有一些示例代码,显示什么不起作用。



查看:
Hello,

I am trying to get my ObservableCollection to raise it's property changed event when one of the items inside of it is changed.

I have some example code below showing what is not working.

View:

推荐答案

Hello Dan,

Hello Dan,

请看看这个案例:

http://stackoverflow.com/questions/1427471/observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyprop

http://stackoverflow.com/questions/1427471/observablecollection-not-noticing-when-item-in-it-changes-even-with-inotifyprop

  RaisePropertyChanged方法不会在您的  innerStuff集合中调用,因为您在集合中更改了值。您可以尝试使用以下代码:

The RaisePropertyChanged method will not be called in your innerStuff collection since you are change value in your collection. You may try use the following code instead:

public Stuff()
        {
            innerStuff = new ObservableCollection<InnerStuff>
            {
                new InnerStuff { number = 1 },
                new InnerStuff { number = 2 },
                new InnerStuff { number = 3 }
            };
            _innerStuff.CollectionChanged += _innerStuff_CollectionChanged;

        }

然后:

 private void _innerStuff_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Object item in e.NewItems)
                {
                    ((INotifyPropertyChanged)item).PropertyChanged += ItemPropertyChanged;
                }
            }
            if (e.OldItems != null)
            {
                foreach (Object item in e.OldItems)
                {
                    ((INotifyPropertyChanged)item).PropertyChanged -= ItemPropertyChanged;
                }
            }
        }

        private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            //This will get called when the property of an object inside the collection changes
        }

最佳问候,

Barry


这篇关于当item的属性发生更改时,在ObservableCollection上引发PropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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