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-->
  <RoutedUICommand x:Key="Remove" Text="Remove">
  <CommandBinding Command="{StaticResource Remove}"

//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

Community On

Try this:

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