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?
Asked
Active
Viewed 9,426 times
7
-
That looks like it could be pretty expensive when, say, building a very large collection. I would hope there is a better option. – kbrimington Aug 16 '10 at 19:42
-
There isn't one that I've seen. – bporter Aug 16 '10 at 19:42
-
What is the purpose of creating your own event instead of using the standard INotifyPropertyChanged/ICollectionChanged interfaces? – Goblin Aug 16 '10 at 19:43
-
@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. – chilltemp Aug 16 '10 at 20:11
-
@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. – Goblin Aug 16 '10 at 20:17
-
@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.) – chilltemp Aug 16 '10 at 21:27
-
Related question: http://stackoverflow.com/questions/17854345/observe-propertychanged-on-items-in-an-observablecollection-using-system-reactiv – Dave Sexton Sep 04 '14 at 21:28
-
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. – code4life Aug 10 '17 at 00:11
2 Answers
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.

Michael L Perry
- 7,327
- 3
- 35
- 34
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
.

Scott Chamberlain
- 124,994
- 33
- 282
- 431