Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ReactiveUI/DynamicData, How to observe changes to properties of items in a SourceList

Tags:

reactiveui

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?

like image 383
dregan Avatar asked Sep 02 '25 16:09

dregan


1 Answers

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 */ }
like image 54
scribblemaniac Avatar answered Sep 04 '25 07:09

scribblemaniac