Example 4-15. Commands with a menu and text box (continued)

<MenuItem Header="Cu_t" Command="ApplicationCommands.Cut" /> <MenuItem Header="_Copy" Command="ApplicationCommands.Copy" /> <MenuItem Header="_Paste" Command="ApplicationCommands.Paste" />

<ToolBarTray DockPanel.Dock="Top"> <ToolBar>

<Button Command="Cut" Content="Cut" /> <Button Command="Copy" Content="Copy" /> <Button Command="Paste" Content="Paste" /> </ToolBar> </ToolBarTray>

Each menu item is associated with a command. This is all that's required to invoke these clipboard operations on the text box; we don't need any code or event handlers because the TextBox class has built-in handling for these commands. More subtly, keyboard shortcuts also work in this example: the built-in cut, copy, and paste commands are automatically associated with their standard keyboard shortcuts, so these work wherever you use a text box. WPF's command system ensures that when commands are invoked, they are delivered to the appropriate target, which in this case is the text box.

You are not obliged to use commands. You may already have classes to represent this idea in your own frameworks, and if WPF's command abstraction does not suit your needs, you can just handle the routed events offered by menu items, buttons, and toolbars instead. But for most applications, commands simplify the way your application deals with user input.

There are five concepts at the heart of the command system: Command object

An object identifying a particular command, such as copy or paste Input binding

An association between a particular input (e.g., Ctrl-C) and a command (e.g., Copy)

Command source

The object that invoked the command, such as a Button, or an input binding Command target

The UI element that will be asked to execute the command—typically the control that had the keyboard focus when the command was invoked

Command binding

A declaration that a particular UI element knows how to handle a particular command

Not all of these features are explicitly visible in Example 4-15—the command bindings are buried inside the text box's implementation, and although input bindings are in use (Ctrl-C will work just fine, for example), they've been set up implicitly by WPF. To make it a bit easier to see all of the pieces, let's look at a slightly more complex example that uses all five concepts explicitly (see Example 4-16).

Example 4-16. Basic command handling

<!-- XAML --> <Window ...> <Grid>

<Button Command="ApplicationCommands.Properties"


// Codebehind public partial class Windowl : Window {

public Windowl() {


InputBinding ib = new InputBinding( ApplicationCommands.Properties, new KeyGesture(Key.Enter, ModifierKeys.Alt)); this.InputBindings.Add(ib);

CommandBinding cb = new CommandBinding(ApplicationCommands.Properties); cb.Executed += new ExecutedRoutedEventHandler(cb_Executed); this.CommandBindings.Add(cb);

void cb_Executed(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("Properties");

This example uses the standard ApplicationCommands.Properties command object. Applications that support this command would typically open a property panel or window for the selected item. The XAML in this example associates a button with this command object; clicking the button will invoke the command. The code behind establishes an input binding so that the Alt-Enter shortcut may also be used to invoke the command. Our example, therefore, has two potential command sources: the button and the input binding. The command target in this particular example will be the button; this is true even if the command is invoked with a keyboard shortcut, because the button is the only element in the window capable of having the keyboard focus. However, the button doesn't know how to handle this command, so it will bubble up to the window, much like an input event. The window does know how to handle the command; it has declared this by creating a command binding with a handler attached to the binding's Executed event. This handler will be called when the user invokes the command.

Now that we've seen all five features in use, we'll examine each one in more detail.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment