Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine "canExecute" from the CommandParameter with ReactiveUI?

Tags:

c#

reactiveui

I can create a command like this which checks for some property of my view model if it can execute:

AddNewSectionCommand = ReactiveCommand
    .CreateFromTask(() => AddNewSectionAsync(NewSectionTitle),
                    this.WhenAny(x => x.NewSectionTitle, x => !string.IsNullOrWhiteSpace(x.Value)));

Is it also possible to do this for the CommandParameter I pass in via WPF, and if, how? Because sometimes I directly want to use something in the UI as the parameter for a command but also want to check validity before. Until now, I have to do this by binding the value to a view model property and use the above which clutters my view models with a lot of superfluous stuff.

like image 816
rabejens Avatar asked Nov 18 '25 16:11

rabejens


1 Answers

No, there’s no CommandParameter for canExecute. The vm property route is the way to go. All the ReactiveUI “getting started” type examples have always used this method (e.g. username/password), which makes sense because the vm should be responsible for determining executability. One of the main reasons being that it’s a lot easier to unit test. So I feel the extra property/properties that get added to the vm is well worth it and not superfluous at all.

like image 170
Colt Bauman Avatar answered Nov 20 '25 06:11

Colt Bauman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!