Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
This topic has not yet been rated - Rate this topic

How to: Hook Up a Command to a Control with No Command Support

The following example shows how to hook up a RoutedCommand to a Control which does not have built in support for the command. For a complete sample which hooks up commands to multiple sources, see the Create a Custom RoutedCommand sample.

Example

Windows Presentation Foundation (WPF) provides a library of common commands which application programmers encounter regularly. The classes which comprise the command library are: ApplicationCommands, ComponentCommands, NavigationCommands, MediaCommands, and EditingCommands.

The static RoutedCommand objects which make up these classes do not supply command logic. The logic for the command is associated with the command with a CommandBinding. Many controls in WPF have built in support for some of the commands in the command library. TextBox, for example, supports many of the application edit commands such as Paste, Copy, Cut, Redo, and Undo. The application developer does not have to do anything special to get these commands to work with these controls. If the TextBox is the command target when the command is executed, it will handle the command using the CommandBinding that is built into the control.

The following shows how to use a Button as the command source for the Open command. A CommandBinding is created that associates the specified CanExecuteRoutedEventHandler and the CanExecuteRoutedEventHandler with the RoutedCommand.

First, the command source is created. A Button is used as the command source.

<Button Command="ApplicationCommands.Open"
        Height="50" Width="200">
  Open (KeyBindings: Ctrl-R, Ctrl-0)
</Button>

// Button used to invoke the command
Button CommandButton = new Button();
CommandButton.Command = ApplicationCommands.Open;
CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)";
MainStackPanel.Children.Add(CommandButton);

Next, the ExecutedRoutedEventHandler and the CanExecuteRoutedEventHandler are created. The ExecutedRoutedEventHandler simple opens a MessageBox to signify that the command executed. The CanExecuteRoutedEventHandler sets the CanExecute property to true. Normally, the can execute handler would perform more robust checks to see if the command could execute on the current command target.


void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("The command has been invoked.");
}
void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
}


Finally, a CommandBinding is created on the root Window of the application that associates the routed events handlers to the Open command.

<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>

// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);

See Also

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.