如何检测ObservableCollection< T>中包含的项目的属性变化. [英] How to detect property changes in items contained in ObservableCollection<T>

查看:86
本文介绍了如何检测ObservableCollection< T>中包含的项目的属性变化.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 private ObservableCollection<Employee> models = new ObservableCollection<Employee>();

我的模型有2个字段(Name和一个名为activeDuty的布尔字段)

My model has 2 fields (Name and a boolean field called activeDuty)

在我的构造函数中,我

 this.models.CollectionChanged += this.OnCollectionChanged;


void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
void OnItemPropertyChanged(object sender, PropertyChangedEventArgs e)

我从没使用过ObservableCollection,有人可以告诉我如何检测activeDuty字段是否被修改?(我做了一些挖掘工作,看到很多关于OnCollectionChanged和OnItemPropertyChanged的帖子,但不了解其中的区别,或者为什么有人偏爱它而不是首选另一个

I've never used an ObservableCollection, could someone tell me how I would detect if the activeDuty field was modified?(I did some digging and saw many posts about OnCollectionChanged and OnItemPropertyChanged but didnt understand the difference or why one is preferred over another

推荐答案

ObservableCollection.CollectionChanged. ObservableCollection还实现了INotifyPropertyChanged,仅用于引发有关其自己的个人财产更改的通知-因此,当添加项目时,还将为其Count属性引发PropertyChanged事件.或删除(您现在有零个理由在乎它,但我们不妨将其扔在那里,以求物有所值).

ObservableCollection.CollectionChanged is raised when an item is added to, or removed from, the collection. ObservableCollection also implements INotifyPropertyChanged, only to raise notifications for changes of its own personal properties -- and so will also raise a PropertyChanged event for its Count property when an item is added or removed (you've got zero reason to care about that right now, but we may as well toss it out there for what it's worth).

因此:当ObservableCollectionObservableCollection的被包含者之一的属性发生更改时,无论该被包含者是否实现INotifyPropertyChanged,它都不会引发任何事件.被容器应该本身实现INotifyPropertyChanged并在其自身属性值更改时引发事件-但是包含它的ObservableCollection不会监听那些事件.我们不需要将绝对的所有情况通知绝对的所有人.

So: Your ObservableCollection of Employee won't raise any events when one of its containees has a property change, regardless of whether or not the containee implements INotifyPropertyChanged. The containee should implement INotifyPropertyChanged itself, and raise events when its own property values change -- but an ObservableCollection containing it won't listen for those events. We don't need to notify absolutely everybody about absolutely everything.

但是确实需要知道activeDuty何时更改.容易.

But you do need to know when activeDuty changes. Easy.

创建新的Employee实例时,可以使用OnItemPropertyChanged处理程序处理它们的PropertyChanged事件:

When you create new Employee instances, you could handle their PropertyChanged events with your OnItemPropertyChanged handler:

//  Fred's not what you'd call active.
var fred = new Employee { Name = "Fred", activeDuty = false };

fred.PropertyChanged += OnItemPropertyChanged;

models.Add(fred);

如果Employee正确实现了INotifyPropertyChanged,则在OnItemPropertyChanged中将立即感知到Fred活动水平的任何可检测到的增加. object sender参数将是Fred,而e.PropertyName将是字符串"activeDuty".

If Employee implements INotifyPropertyChanged correctly, any detectable increase in Fred's activity level will be perceived immediately in OnItemPropertyChanged. The object sender parameter will be Fred, and e.PropertyName will be the string "activeDuty".

public class Employee : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private bool _activeDuty = false;
    public bool activeDuty {
        get { return _activeDuty; }
        set {
            _activeDuty = value;
            PropertyChanged?.Invoke(this, 
                new PropertyChangedEventArgs(nameof(activeDuty)));
        }
    }

    //  Name implemented similarly -- either that, or give it a protected 
    //  setter and initialize it in the constructor, to prevent accidents.
}

我认为您不需要处理models.CollectionChanged,除非可以向其中添加其他随机的视图模型.如果可能的话,那么这是将PropertyChanged处理程序放置在新的Employee上的非常方便的地方.

I don't think you need to be handling models.CollectionChanged unless random other viewmodels could be adding to it. If they could be, then that's a very handy place to put PropertyChanged handlers on new Employees.

这篇关于如何检测ObservableCollection&lt; T&gt;中包含的项目的属性变化.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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