简体   繁体   English

如何为集合中对象的特定属性设置属性更改事件

[英]How to set Property Changed Event for a Particular Property of an Object in Collection

I have an ObservableCollection which has objects. 我有一个ObservableCollection其中有对象。 My requirement is I want to set Property Changed For a particular Property to trigger Collection Changed event. 我的要求是我要为特定属性设置“属性已更改”,以触发“收藏集更改”事件。

I can set Property changed for the object like: 我可以将对象的属性设置为:

foreach (INotifyPropertyChanged prop in _baseColl)
{
 prop .PropertyChanged += prop_PropertyChanged;
}

prop_PropertyChanged()
{
//Will Refresh My Collection.
}

This case It will be called for all the properties in that Object.But i dont want that. 在这种情况下,将为该Object.All中的所有属性调用它,但我不希望这样。

PS : I also Know that we can trigger the CollectionChanged By "Add","Remove","Move" etc..But i want another solution in my case. PS:我也知道我们可以通过“添加”,“删除”,“移动”等来触发CollectionChanged。但是我需要另一种解决方案。

All you can do if you're handling PropertyChanged is to check the "PropertyName"; 如果要处理PropertyChanged您所能做的就是检查“ PropertyName”。

prop_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "TheProperty")
    {
        //Will Refresh My Collection.
    }
}

This should be fine for most cases. 在大多数情况下,这应该没问题。 If you want a more narrowly-targeted invocation, then you can always consider using a custom event -- have the source object raise that event only when the property you are interested in changes. 如果您希望进行更窄范围的调用,则可以始终考虑使用自定义事件-仅当您对属性感兴趣时才让源对象引发该事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM