Reputation: 14299
I have a command (ReactiveCommand
) and I want to execute this command when a user selects an item in a list.
A list exposes an observable IObservable<object>
so each time there is a new event sent to this observable I want to execute my command.
I came with this but it seems quite complicated for what it does.
source.ElementSelected
.Cast<Item>()
.SelectMany(ViewModel.ShowDetailsCommand.Execute)
.Subscribe();
I wonder if there is a better way to do it? Is there something like BindCommand
that exists for commands and controls?
Upvotes: 1
Views: 657
Reputation: 2177
Usually I'll have what you have but using WhenAnyObservable or similar. Most of the samples in RxUI also use similar syntax with chaining an observable into an Execute.
Though InvokeCommand is one way to simplify down a little bit.
https://reactiveui.net/docs/handbook/commands/invoking-commands
Otherwise would probably just need to make your own extension method.
Also I just want to make sure to point out
InvokeCommand respects the command's executability. That is, if the command's CanExecute method returns false, InvokeCommand will not execute the command when the source observable ticks.
Upvotes: 1