I have created an ICommand in my MVVM viewmodel. And this ICommand applies to different buttons using some parameters each time. Specifically two parameters. And I want to raise that command from code behind and also set the values to those two command parameters.
<Button
Content="button 1"
Name="butonn_1"
Command="{Binding CustomCommand}">
<Button.CommandParameter>
<local:MyCommandParameter
MyString="Nick"
MyInt="12"/>
</Button.CommandParameter>
The ICommand
is called CustomCommand
and receives two CommandParameters MyString
and MyInt
ICommand and the parameters
public class MyCommandParameter
{
public int MyInt { get; set; }
public string MyString { get; set; }
}
public ICommand ViewTableCommand
{
get { return new DelegateCommand<object>(FuncToCall); }
}
public void FuncToCall(object parameter)
{
var param = (MyCommandParameter)parameter;
Debug.WriteLine($"Name: {param.MyString} and Age: {param.MyInt}";
}
Now I have created another method that calls the ICommand whenever I want. But each time the ICommand should get different values for each CommandParameters. For example,
public void RaiseCommandButton(Button ButtonName)
{
ButtonName.Command.Execute(ButtonName.CommandParameter);
}
This is how I raise the ICommand any times I want. But until now I can give only one pair of CommandParameters. So for example I would like to do something like below
//Pseudo code - does not work but describes what I want to achieve
public void RaiseCommandButton(Button ButtonName)
{
ButtonName.Command.Execute(ButtonName.CommandParameter(MyString: "Nikos", MyInt:"12");
//...rest code
}
Trying to solve this, I searched various links like:
But none of those links helped to answer my question. I would really appreciate the help of the community.