Reacting to ReactiveCommand in DynamicData collection

140 views Asked by At

As I am trying to remove old implementations of ReactiveList, I find myself trying to implement a way to remove an item from a DynamicData collection when a ReactiveCommand in the said item is triggered.

The old implementation worked like this:

OldReactiveList
  .ObserveProperty(vm => vm.RemoveCommand)
  .Subscribe(vm => OldReactiveList.Remove(vm));

Is there a way to implement a similar structure with an observable change set from a DynamicData SourceList?
Everything I've tried so far seems to fail to either not knowing which item triggered the observation, or the observing relying on the observable property actually changing.

1

There are 1 answers

0
vipirtti On BEST ANSWER

The cleanest solution I have come up with would be to modify the originally "empty" remove command to return "self":

public class ListItemViewModel : ReactiveObject
{
    public ReactiveCommand<Unit, ListItemViewModel> RemoveViewModel { get; }

    public ListItemViewModel()
    {
        RemoveViewModel = ReactiveCommand.Create(() => this);
    }
}

And then use SubscribeMany to listen to these commands and get the item to remove from the list:

ViewModelList = ObservableCollectionExtended<ListItemViewModel>;
ViewModelList.ToObservableChangeSet()
    .SubscribeMany(vm => vm.RemoveViewModel.Subscribe(x => ViewModelList.Remove(x)))
    .Subscribe();

The ViewModelList could be implemented via SourceList or SourceCache, but the solution would be mostly the same. Only the way to access the change set would change.