I am trying to pass two ICommand parameters on button click to a method in view model.
Now I am able to pass only one parameter.
Code here.
XAML (View):
<Button x:Name="btnAdd" Command="{Binding AddUserCommand}"
CommandParameter="{Binding IDUser}"/>
View Model:
public string IDUser
{
get
{
return this.personalData.UserID;
}
set
{
if (this.personalData.UserID == value)
{
return;
}
this.personalData.UserID = value;
OnPropertyChanged("UserID");
}
}
private RelayCommand addUserCommand;
public ICommand AddUserCommand
{
get
{
return addUserCommand ??
(addUserCommand = new RelayCommand(param => this.AddUser(param.ToString())));
}
}
public vol AddUser(string userId)
{
// Do some stuff
}
Now I want to pass another ICommand parameter on button click. The parameter I want to pass is the value (checked or not) from a checkbox.
<CheckBox x:Name="Status" Content="Married"/>
so that method AddUser in view model will have below signature:
public vol AddUser(string userId, bool status)
{
// Do some stuff
}
I know it can be done using MultiBinding in combination with a converter but I do not know exactly how to do it. Also I am not understanding at all why a converter is necessary when using multibinding.
A command takes only one parameter. The
Execute
method of theICommand
interface is defined as:So you can't pass two parameters to a command.
What you could do is to pass an instance of a class that has two properties. This is where the multi converter comes in.
Bind the
CommandParameter
to two properties and use the converter to return one object.XAML:
Converter:
View Model:
Of course you must also define the
YourClass
payload type with the two parametersIDUser
andIsChecked
.