TAGS :Viewed: 2 - Published at: a few seconds ago

[ DataTemplate button command from binding ]

I'm using Wpf and I'm passing a List<Value> to a <ItemsControl> in the xaml. I would like to bind the string in the Value Object to the Command of a Button. This xaml part looks like this:

    &lt;Grid Margin="0,0,2,0"&gt;
    &lt;Grid Margin="10"&gt;
        &lt;ItemsControl Name="details"&gt;
            &lt;ItemsControl.ItemTemplate&gt;
                &lt;DataTemplate&gt;
                    &lt;Grid Margin="0,0,0,5"&gt;
                        &lt;Grid.ColumnDefinitions&gt;
                            ....
                        &lt;/Grid.ColumnDefinitions&gt;
                        ...
                        &lt;Button Grid.Column="2"
                                Content="{Binding ButtonContent}"
                                Visibility="{Binding ButtonVisibility}"
                                Command="{Binding ButtonClickMethod}" /&gt;
        ...

My Value Class looks like this:

public class Value
{        
    ...
    public string ButtonClickMethod { get; set; }

}

I'm setting the string link this:

v.ButtonClickMethod = "RelatedActivityId_OnClick";

And the Method is in the same class and looks like this:

private void RelatedActivityId_OnClick(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("RelatedActivityId_OnClick");
    }

Everything besides this is working properly and unses the same Object for the binding. What am I doing wrong?

Answer 1


The Command property of the Button is of type ICommand so you cannot bind it to a string value.

You need to update your ButtonClickMethod to be of type ICommand or create a new property to bind you Command to.

See this answer for a sample implementation of ICommand.

If you need the button to execute code based on a parameter (string value?) then you can use the CommandParameter property, then use that paramters in your Command handler.

public class Value
{        
    public Value()
    {
        ButtonCommand  = new RelayCommand((a) => true, CommandMethod); 
    }

    public RelayCommand ButtonCommand {get; set; }
    public string ButtonClickMethod { get; set; }

    private void CommandMethod(object obj)
    {
        MessageBox.Show(obj?.ToString());
    }
}

and the XAML:

<Button Grid.Column="2"
         Content="{Binding ButtonContent}"
         Visibility="{Binding ButtonVisibility}"
         Command="{Binding ButtonCommand}"
         CommandParameter="{Binding ButtonClickMethod}" />

Answer 2


The Button.Command property binds only to objects which implement the ICommand interface. If you want to invoke a method which its name is ButtonClickMethod, you will have to:

  1. Create a class which implements ICommand interface.
  2. Create a object of that class and bind it to your button (bind it to Button.Command).
  3. Pass the Value.ButtonClickMethod as a CommandParameter to your ICommand object.
  4. Use this to invoke any method you would like to.