Each item in the list implements ReactiveObject so I've tried using item.WhenAnyValue().Subscribe() on each individual item before adding it to the SourceList. While this works, it has terrible performance and isn't really practical for my SourceList of 40000 items. Is there any way to observe the SourceList once for changes to properties of any items in the list?
DynamicData provides several extensions to IObservable<IChangeSet<T>>
which you can use to bind to all of the items in your SourceList
. All of these will gracefully handle added/removed items from your list.
WhenValueChanged
This is basically the equivalent of WhenAnyValue
but for lists and is probably what you're looking for. This will return an observable of the value when a target property has changed (and optionally when initialized). Example:
sourceList
.Connect()
.WhenValueChanged(item => item.property)
.Subscribe(newPropertyValue => { /* Do stuff */ }
WhenPropertyChanged
Similar to ObservableForProperty
, this will return an observable of the value of target property and its parent object, when it has changed (and optionally when initialized). Example:
sourceList
.Connect()
.WhenPropertyChanged(item => item.property)
.Subscribe(change => { /* Do stuff with change.Sender and change.Value */ }
MergeMany
The most general option, MergeMany
allows you to combine observables created or retrieved from each of your list items. For example, to accomplish something similar to WhenValueChanged
you could do:
sourceList
.Connect()
.MergeMany(item => item.WhenAnyValue(x => x.property))
.Subscribe(newPropertyValue => { /* Do stuff */ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With