A command takes only one parameter. The Execute
method of the ICommand
interface is defined as:
void Execute(object parameter);
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:
<Button x:Name="btnAdd" Command="{Binding AddUserCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource yourConverter}">
<Binding Path="IDUser" />
<Binding Path="IsChecked" ElementName="Status" />
</MultiBinding>
</Button.CommandParameter>
</Button>
Converter:
public class Converter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return new YourClass() { IdUser = values[0] as string, IsChecked = System.Convert.ToBoolean(values[1]) };
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
View Model:
private RelayCommand<YourClass> addUserCommand;
public ICommand AddUserCommand
{
get
{
return addUserCommand ??
(addUserCommand = new RelayCommand<YourClass>(param => this.AddUser(param)));
}
}
public vol AddUser(YourClass obj)
{
string IDUser = obj.IDUser;
bool isChecked = obj.IsChecked;
// Do some stuff
}
Of course you must also define the YourClass
payload type with the two parameters IDUser
and IsChecked
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…