Listen to the PropertyChanged event of any item in an ObservableCollection<INotifyPropertyChanged> [duplicate]
Asked Answered
B

2

7

I've created a wrapper collection for ObservableCollection that subscribes to each items PropertyChanged event and rethrows it as its own event ItemPropertyChanged. I did this using a similar method to what I described here. Is there a better way? Am I missing another .NET collection that already has this type of behavior?

Breastpin answered 16/8, 2010 at 19:37 Comment(8)
That looks like it could be pretty expensive when, say, building a very large collection. I would hope there is a better option.Caducous
There isn't one that I've seen.Upper
What is the purpose of creating your own event instead of using the standard INotifyPropertyChanged/ICollectionChanged interfaces?Counterspy
@Goblin: The new event includes the name item's property that changed. That can't be conveyed properly on the existing event. Consumers of the collections PropertyChanged event would expect the name to be one of the collections properties.Breastpin
@chilltemp: What I'm fishing at, is why would the consumers of the collection want to know about item-updates? I fail to understand the reasoning behind adding a second event-handling routine to an existing notification strategy. IMO it reeks of needless complexity. Consumers of an ObservableCollection can listen to property-updates for the items already (that's what ItemsControls in WPF are already doing out of the box.Counterspy
@Goblin: I this case I want to trigger the resorting of a DataGrid whenever the data in the sorted column changed, but this is not the first time I've needed some form of child item notification. (And yes, I'm aware of the potential performance problem if there are frequent updates to the sorted column.)Breastpin
Related question: #17854845Ionic
Make the items in the collection themselves to be INotifyPropertyChanged. Can't see any other way that you can do this, outside of Rx, potentially. Don't worry too much about perf issues. In fact, you should be downloading the WPF profiling suite from the MSDN site and validating perf from that. Just going by the platitudes isn't going to cut it.Expel
P
0

I'm assuming that you are firing this event in order to compute an aggregate. I have a different solution to this problem. Consider using Update Controls with linq. You can declaratively describe your aggregate with linq, and Update Controls will track its dependencies within your collection. Whenever the collection changes, or any of the referenced properties changes, then it will reevaluate the aggregate.

Potiche answered 13/9, 2012 at 20:3 Comment(0)
O
0

Just use System.ComponentModel.BindingList<T> instead. The ListChanged event it has will fire when any item in the list fires it's INotifyPropertyChanged.

Oireachtas answered 7/2, 2018 at 22:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.