Keyboard and Ink Input with WPF - Commands
(Page 4 of 4 )
The input events we’ve examined give us a detailed view of user input directed at individual elements. However, it is often helpful to focus on what the user wants our application to do, rather than how she asked us to do it. WPF supports this through the command abstraction—a command is an action the application performs at the user’s request.
The way in which a command is invoked isn’t usually important. Whether the user presses Ctrl-C, selects the Edit -> Copy menu item, or clicks the Copy button on the toolbar, the application’s response should be the same in each case: it should copy the current selection to the clipboard. The event system we examined earlier in this chapter regards these three types of input as being unrelated, but WPF’s command system lets you treat them as different expressions of the same command.
The command system lets a UI element provide a single handler for a command, reducing clutter and improving the clarity of your code. It enables a more declarative style for UI elements; by associating aMenuItemorButtonwith a particular command, you are making a clearer statement of the intended behavior than you would by wiring upClick event handlers. Example 4-15 illustrates how commands can simplify things.
Example 4-15. Commands with a menu and text box
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_Edit">
<MenuItem Header="Cu_t" Command="ApplicationCommands.Cut" />
<MenuItem Header="_Copy" Command="ApplicationCommands.Copy" />
<MenuItem Header="_Paste" Command="ApplicationCommands.Paste" />
</MenuItem>
</Menu>
<ToolBarTray DockPanel.Dock="Top">
<ToolBar>
<Button Command="Cut" Content="Cut" />
<Button Command="Copy" Content="Copy" />
<Button Command="Paste" Content="Paste" />
</ToolBar>
</ToolBarTray>
<TextBox />
</DockPanel>
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 theTextBoxclass 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"
Content="_Properties"/>
</Grid>
</Window>
// Codebehind
public partial class Window1 : Window {
public Window1(){
InitializeComponent();
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 standardApplicationCommands.Propertiescommand 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’sExecuted 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.
Please check back next week for the continuation of this series.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from Programming WPF, Second Edition, written by Chris Sells and Ian Griffiths (O'Reilly, 2007; ISBN: 0596510373). Check it out today at your favorite bookstore. Buy this book now.
|
|