I've an issue with adding a UIcommand to the button.

I tried to add command like I did before with .Click action method, like "button.Click += CommandBinding_Remove_Executed;", but failed.

<!--Declaration in XAML-->
<Window.Resources>
  <RoutedUICommand x:Key="Remove" Text="Remove">
    <RoutedUICommand.InputGestures>
      <KeyGesture>CTRL+R</KeyGesture>
    </RoutedUICommand.InputGestures>
  </RoutedUICommand>
</Window.Resources>
<Window.CommandBindings>
  <CommandBinding Command="{StaticResource Remove}"
                  Executed="CommandBinding_Remove_Executed"/>
</Window.CommandBindings>

//Using in C#
Button button = new Button;
button.Command += CommandBinding_Remove_Executed; //An issue

I expected that it should add an execution method of that command, but there's an error type not delegate of ICommand.

1 Answers

0
Community On

Try this:

Binding binding = new Binding();
binding.Path = new PropertyPath("YourCommandName");
button.SetBinding(Button.CommandProperty, binding);