Home > OS >  How can I pass multiple command parameters to a button WPF?
How can I pass multiple command parameters to a button WPF?

Time:03-18

To set up the context :

I have a datagrid with multiple data types inside. For each type, I have a custom data template for the details to fit my data. In the details, I have a little form with a text box and 2 buttons. The objective is to get the data of my row and the content of my text box when I click the buttons.

Curently, I have created a class with those parameters and I am trying to pass this class to my command parameter this way :

<Button Content="Validate" Width="70" Command="{Binding Source={StaticResource btnVmValidateReserved}, Path=ButtonValidateReservedCommand}">
    <Button.CommandParameter>
        <model:ButtonValidateReservedCommandArgs Item="{Binding Data}" Message="{Binding Path=Text, ElementName=umMasterComment}"/>
    </Button.CommandParameter>
</Button>

Here is my class :

public class ButtonValidateReservedCommandArgs : DependencyObject
{
    public static readonly DependencyProperty ItemProperty = DependencyProperty.Register("Item", typeof(object), typeof(ButtonValidateReservedCommandArgs));
    public object Item
    {
        get { return GetValue(ItemProperty); }
        set { SetValue(ItemProperty, value); }
    }

    public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(ButtonValidateReservedCommandArgs));
    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue (MessageProperty, value); }
    }
}

When I try with bindings, I always have null as a result. But when I try to do it with a random text, it works.

Also, when i try to do it this way it works :

<Button Content="Validate" Width="70" Command="{Binding Source={StaticResource btnVmValidateReserved}, Path=ButtonValidateReservedCommand}" CommandParameter="{Binding Data}"/>

However, I need my 2 arguments and I can’t figure out to do it.

If you have any idea to have a working binding, or even another solution to pass my 2 arguments, I’ll take it

  • Related