0

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_button_Executed;", but failed.

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

public partial class MainWindow : Window
{
    //Using in C#
    Button button = new Button;
    button.Command += CommandBinding_button_Executed; //An issue

    //Puts button in form
    Grid.SetRow(button, 0);
    Grid.SetColumn(button, 0);
    grid.Children.Add(button);

    private void CommandBinding_BtnCmd_Executed(object sender, ExecutedRoutedEventArgs e)
    {
      //some code
    }
}

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

  • 1
    Your mindset is in *WinForms* mode. You need to shift it to *WPF* mode. This may help... https://stackoverflow.com/questions/4101729/wpf-architecture-confusion-re-routed-commands-events-and-gestures – Richardissimo Jun 02 '19 at 01:31

1 Answers1

0

Try this:

Binding binding = new Binding();
binding.Path = new PropertyPath("YourCommandName");
button.SetBinding(Button.CommandProperty, binding);
AmRo
  • 833
  • 1
  • 8
  • 19