1

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.

DelusionX
  • 79
  • 8

1 Answers1

1

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.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • Peter thanks a lot for your answer. Indeed your correction points are clear to me and I am aware of the properties ```MyString``` and ```MyInt```. Now to get it clear inside my head. ```Execute()``` can receive a method and instantly translate it into a CommandParameter. Based on the second link I posted, I was trying to find a way to set the values for the property ```ButtonBase.CommandParameter```. This is where I lost it so I had the need to post a question. I initially thought that I can get, set the value of a property. – DelusionX Nov 16 '20 at 07:37
  • _"I initially thought that I can get, set the value of a property"_ -- it is possible to do it that way. But that only directly would affect when the button itself is clicked. The code you posted, you're not doing anything with the button except using it to hold the `ICommand` and its parameter value, so there's no reason to put the parameter value in the `CommandParameter` property. You can just pass it directly to the `Execute()` method when you call it. – Peter Duniho Nov 16 '20 at 17:01
  • "*But that only directly would affect when the button itself is clicked*" -- the user is supposed to click the button. So the button, yes, will be eventually clicked. My inexperience prevented me from finding a valid way to affect the ```{set;}``` property of the method. But if your approach can solve my question it is sufficient for the moment. I haven't tested yet the code but I will do. – DelusionX Nov 17 '20 at 08:06
  • _"the button, yes, will be eventually clicked"_ -- that's not what the code in your question shows. You wrote _"I want to raise that command from code behind"_, which is a completely different scenario from the user clicking the button. Do you want the command to be executed _twice_ when the user clicks, once with each parameter value? The answer I posted answers the question you asked. If you have a different question, you should post that as a separate post, rather than making changes to this question (whether by editing the post or in these comments). – Peter Duniho Nov 17 '20 at 17:00
  • "*Do you want the command to be executed twice when the user clicks, once with each parameter value?*" - No. Peter, I may typed this wrong. The command will be executed once the button is clicked. And when the button is clicked, the command binded to the button, will take the pair of values defined from the code behind. I firmly believe your answer suffices my question. Although, I haven't tested it yet because I need some time to make the changes. I will keep you post. – DelusionX Nov 17 '20 at 17:50
  • I may confused you in the first place because I use two different pairs of values. My mistake Imagine 1 pair of values executed in code behind. See my latest update :) Apologize for the inconvenience – DelusionX Nov 17 '20 at 18:01