Svein Terje Gaup
Svein Terje Gaup

Reputation: 1578

WPF MVVM Using the same Command with CommandParameter for handling more than one button

this might be quite an easy question, but I guess I need some help anyway.

Using WPF MVVM. This is code from my view.

<Button Command="{Binding SetStatusBEH}" CommandParameter="BEH" HorizontalAlignment="Stretch" Margin="1" Padding="4">Set status BEH</Button>
<Button Command="{Binding SetStatusINN}" CommandParameter="INN" HorizontalAlignment="Stretch" Margin="1" Padding="4">Set status INN</Button>
<Button Command="{Binding SetStatusUTG}" CommandParameter="UTG" HorizontalAlignment="Stretch" Margin="1" Padding="4">Set status UTG</Button>
<Button Command="{Binding SetStatusOUT}" CommandParameter="OUT" HorizontalAlignment="Stretch" Margin="1" Padding="4">Set status OUT</Button>

As you may see, I am using a different binding on each button. It works.

My handlers (in the ViewModel) are like this:

public Command SetStatusBEH => _setStatusBEH ?? (_setStatusBEH = new Command(a => DoSetStatusBEH()));
public Command SetStatusINN => _setStatusINN ?? (_setStatusINN = new Command(a => DoSetStatusINN()));
public Command SetStatusUTG => _setStatusUTG ?? (_setStatusUTG = new Command(a => DoSetStatusUTG()));
public Command SetStatusOUT => _setStatusOUT ?? (_setStatusOUT = new Command(a => DoSetStatusOUT()));

I got a comment on my PR that I should be using the same handler on all the buttons, differing by, I guess, sending a different CommandParameter. So I have added CommandParameters, as can be seen.

But how do I get the parameter? It is not possible to bind to a method which has the argument, the compiler doesn't like it, says it must be a property not a method. So how do I get at the argument/parameter?

Upvotes: 0

Views: 94

Answers (1)

Emperor Eto
Emperor Eto

Reputation: 3575

Taking an educated guess that a is indeed going to receive your CommandParameter from XAML when the button is clicked, in which case this is what you'd do:

XAML:

<Button Command="{Binding SetStatus}" CommandParameter="BEH" HorizontalAlignment="Stretch" Margin="1" Padding="4">Set status BEH</Button>
<Button Command="{Binding SetStatus}" CommandParameter="INN" HorizontalAlignment="Stretch" Margin="1" Padding="4">Set status INN</Button>
<Button Command="{Binding SetStatus}" CommandParameter="UTG" HorizontalAlignment="Stretch" Margin="1" Padding="4">Set status UTG</Button>
<Button Command="{Binding SetStatus}" CommandParameter="OUT" HorizontalAlignment="Stretch" Margin="1" Padding="4">Set status OUT</Button>

ViewModel:

public Command SetStatus => _setStatus ?? (_settStatus = new Command(a => DoSetStatus(a as string)));

private void DoSetStatus(string para)
{
  // will be "BEH" "INN" "UTG" or "OUT"
}

If this doesn't literally work right away, try breaking inside the command handler to see what a is. If it's not a string it should be something that at least allows you to get the CommandParameter in an obvious way.

Upvotes: 1

Related Questions