More on Commands, Input and the WPF
(Page 1 of 4 )
In this conclusion to a five-part article series on the input handling mechanisms available in WPF, you'll learn about command bindings, enabling and disabling commands, and more. It is excerpted from
Programming WPF, Second Edition, written by Chris Sells and Ian Griffiths (O'Reilly, 2007; ISBN: 0596510373). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Command Bindings
For a command to be of any use, something must respond when it is invoked. Some controls automatically handle certain commands—the TextBox and RichTextBox handle the copy and paste commands for us, for example. But what if we want to provide our own logic to handle a particular command?
Command handling is slightly more involved than simply attaching a CLR event handler to a UI element. The classes in Table 4-4 define 144 commands, so ifFrameworkElementdefined CLR events for each distinct command, that would require 288 events once you include previews. Besides being unwieldy, this wouldn’t even be a complete solution—many applications define their own custom commands as well as using standard ones.
The obvious alternative would be for the command object itself to raise events. However, each command is a singleton—there is only oneApplicationCommands.Copyobject, for example. If you were able to add a handler to a command object directly, that handler would run anytime the command was invoked anywhere in your application. What if you want to handle the command only if it is executed in a particular window or within a particular element?
TheCommandBindingclass solves these problems. ACommandBindingobject associates a specific command object with a handler function in the scope of a particular user interface element. ThisCommandBindingclass offersPreviewExecutedandExecutedevents, which are raised as the command tunnels and bubbles through the UI.
Command bindings are held in theCommandBindings collection property defined byUIElement. Example 4-25 shows how to handle the ApplicationCommands.Newcommand in the code behind for a window.
Example 4-25. Handling a command
public partial class Window1 : Window {
public Window1() {
InitializeComponent();
CommandBinding cmdBindingNew = new CommandBinding(ApplicationCommands.New);
cmdBindingNew.Executed += NewCommandHandler;
CommandBindings.Add(cmdBindingNew);
}
void NewCommandHandler(object sender, ExecutedRoutedEventArgs e){
if (unsavedChanges) {
MessageBoxResult result = MessageBox.Show(this,
"Save changes to existing document?", "New",
MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Cancel) {
return;
}
if (result == MessageBoxResult.Yes) {
SaveChanges();
}
}
// Reset text box contents
inputBox.Clear();
}
...
}
Next: Enabling and disabling commands >>
More .NET Articles
More By O'Reilly Media
|
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.
|
|