WPF Basics - Commands

The WPF architects did not simply want to bring to XAML the standard programming model of controls and events. They wanted to address and simplify common scenarios faced by developers. One such scenario is having multiple input mechanisms that the user can do to perform an action.

For example, to Open a File, users often can:

  1. Click an Open icon on the toolbar
  2. Select the File->Open menu option
  3. Use a keyboard shortcut, such as Ctrl+O
A developer would typically write three different event handlers to subscribe to the various input mechanisms, and then pass off the action to a common method. This relationship to a common implementation, however, cannot be represented in XAML.

This is where Commands come in.

Commands allow developers to indicate that various controls, keyboard input, and mouse input all route to the same logical command, and that the command should be handled by a particular method. To set up a command, follow these three steps:

  1. Identify the built-in or custom Command you will be binding to (eg. ApplicationCommands.Open)
  2. Associate one or more input sources to the command
  3. Create a CommandBinding to map the Command to methods
1 <Canvas> 2 <!-- Step 3: Command is associated with methods --> 3 <Canvas.CommandBindings> 4 <CommandBinding Command="ApplicationCommands.Open" CanExecute="CommandBindingOpen_CanExecute" 5 Executed="CommandBindingOpen_Executed" /> 6 </Canvas.CommandBindings> 7 8 <!-- Step 1 & 2: ApplicationCommands.Open, with three input sources: the button, pressing Ctrl+O, 9 or clicking the middle mouse button --> 10 <Canvas.InputBindings> 11 <KeyBinding Command="ApplicationCommands.Open" Key="O" Modifiers="Control" /> 12 <MouseBinding Command="ApplicationCommands.Open" Gesture="MiddleClick" /> 13 </Canvas.InputBindings> 14 <Button Command="ApplicationCommands.Open" Name="button2">Open</Button> 15 </Canvas> 16
In the code behind are the methods referenced in the CommandBinding element. The CanExecute method is used to determine when the command can be executed. The Executed method is used whe the command is invoked by any of the input methods.
private void CommandBindingOpen_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } private void CommandBindingOpen_Executed(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("Open Command Executed"); }

Built-In Commands

There are over 150 built-in commands with WPF, divided into 5 categories and implemented as static properties on these 5 classes in the System.Windows.Input namespace:
  1. ApplicationCommands - common commands in applications, such as Open, Save, Close, Cut, Copy, Paste
  2. ComponentCommands - commands for moving, scrolling, selecting, such as ScrollPageDown
  3. MediaCommands - commands when using media, such as Play, Stop, IncreaseVolume, DecreaseVolume
  4. NavigationCommands - commands for page navigation, such as NextPage, BrowseHome, Stop
  5. EditingCommands - commands for document editing, such as AlignLeft, MoveToLineEnd, ToggleBold
Copyright 2008 - www.WPFDude.com