Reputation: 79
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.
Upvotes: 1
Views: 425
Reputation: 70671
In your original code, you are passing the CommandParameter
value you declared in XAML. That's why you use the CommandParameter
property. But that property is just that: a property, that you can retrieve the value of. It's not a method that you can pass argument values to.
In the code you posted, there is also the issue that "
is the correct delimiter for a string
literal, not '
.
Seems like you should be able to just create a new MyCommandParameter
instance for each call you want to me. For example:
public void RaiseCommandButton(Button ButtonName)
{
ButtonName.Command.Execute(new MyCommandParameter { Name = "Nikos", Age = 12 });
//...code
//Raise command with different pair of Parameters
ButtonName.Command.Execute(new MyCommandParameter { Name = "Alex", Age = 25 });
}
Keeping in mind that in your pseudocode version, you used different property names than the ones you show in the actual declaration of your MyCommandParameter
class. I've reused those property names instead of the original MyString
and MyInt
names. I presume you understand that distinction, and can write the actual implementation using whatever property names are actually suitable for your real code.
Upvotes: 1