Tuesday, March 8, 2011

Command Property

Installing a handler for the button's Click event is certainly the most traditional way for a program to be notified when the user clicks a button. However, it could be that a particular operation in your program can be triggered through several sourcesperhaps a menu item, a toolbar item, and a button. Perhaps you even implement a scripting language that can trigger this same operation. It might make more sense to route all these controls through a single point.

This is the idea behind a property named Command, which is defined by the ButtonBase class and some other classes, including MenuItem.

For common commands, you set the Command property of the Button object equal to a static property of the classes ApplicationCommands, ComponentCommands, MediaCommands, NavigationCommands, or EditingCommands. The first four of these classes are located in the System.Windows.Input namespace; the last is in System.Windows.Documents. The static properties of these classes are all of type RoutedUICommand. You can also create your own objects of type RoutedUICommand.

For example, if you wanted a particular button to perform a clipboard Paste command, you would set the Command property like so:

btn.Command = ApplicationCommands.Paste;




It's not required, but you may also want to set the button text to the standard text for that command:

btn.Content = ApplicationCommands.Paste.Text;




The other step is to associate this particular Paste command with event handlers. This association is called a binding, and it's one of several types of bindings you'll find in the Windows Presentation Foundation. The UIElement class defines (and the Control and Window classes inherit) a property named CommandBindings, which is a collection of CommandBinding objects. You'll probably use the CommandBindings collection of the window, so the statement looks something like this:

CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste,
PasteOnExecute, PasteCanExecute));




PasteOnExecute and PasteCanExecute are two event handlers that are located in your program. (You can name them whatever you want.) In the PasteOnExecute handler, you carry out the Paste command. Sometimes, however, your program can't carry out a Paste command because the clipboard doesn't contain data in the format you want. That's the purpose of the PasteCanExecute handler. This handler checks the clipboard for data of the proper format. If the data isn't available, the handler can set a flag and the Button will automatically be disabled.

If this program also has a menu item for Paste, you just need to assign the Command property of the menu item to ApplicationCommands.Paste, and the same binding will apply.


Button btn = new Button();
btn.HorizontalAlignment = HorizontalAlignment.Center;
btn.VerticalAlignment =VerticalAlignment.Center;
btn.Command = ApplicationCommands.Paste;
btn.Content = ApplicationCommands.Paste.Text;

// Bind the command to the event handlers.
CommandBindings.Add(new CommandBinding
(ApplicationCommands.Paste,
PasteOnExecute,
PasteCanExecute));


Writing the handlers:


void PasteOnExecute(object sender,ExecutedRoutedEventArgs args)
{
Title = Clipboard.GetText();
}
//2nd event handler
void PasteCanExecute(object sender, CanExecuteRoutedEventArgs args)
{
args.CanExecute = Clipboard.ContainsText();
}


The two event handlers PasteOnExecute and PasteCanExecute use static methods from the Clipboard class (located in System.Windows). The static GetText and ContainsText methods both exist in overloaded versions that allow you to be specific about the type of text you wantincluding comma-separated format, HTML, RTF, Unicode, or XAMLbut this program doesn't use those text types. The text it gets from the clipboard is just used to set the window's Title property. Since you may want to experiment with this program a bit, the last method in the program lets you click anywhere in the window to restore the Title text.


Before running the program, you might want to copy some text to the clipboard from Notepad or another text-based application (even Visual Studio) just so the program has something to work with. You'll discover that the button responds not only to the normal keyboard and mouse input, but also to Ctrl+V, the standard keyboard shortcut for Paste. That's one of the benefits of using a command binding rather than just handling the Click event.

Now copy a bitmap into the clipboard. No need to run Paint or PhotoShop! Just press the Print Screen key to copy the screen image into the clipboard. Whenever there's a change in the clipboard, any bound can-execute event handlers (such as PasteCanExecute) are called so that the handler has the opportunity to properly set the CanExecute property of the event arguments. The button is immediately disabled. Now copy some text into the clipboard. The button is enabled again. Behind the scenes, the command binding changes the IsEnabled property that Button inherits from UIElement.

No comments:

Post a Comment