RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Introduction to the WPF Command Framework : Page 2

The command framework in WPF provides a set of powerful tools that let developers build more intelligent user interfaces.

Enter WPF Commands
Here's how you could hook up the first example (the single list box window) with WPF commands.

     <MenuItem Command="ApplicationCommands.Delete"         
       Header="Delete" />
           Executed="DeleteCustomer_Executed" />
Notice that the preceding code assigns a value to the Command property of the Delete menu. It also adds a command binding to the customer ListBox. In this scenario, the Menu is the command source, and the ListBox acts as a command target. The CommandBinding specifies delegates for CanExecute and Executed. Here are their implementations:

   private void DeleteCustomer_CanExecute(
      object sender, CanExecuteRoutedEventArgs e)
      e.CanExecute = lsbCustomers.SelectedItem != null;
   private void DeleteCustomer_Executed(object sender, 
      ExecutedRoutedEventArgs e)
So far, the code doesn't look all that different from the first example. However, the benefits of this approach become clearer when you add the second "Products" list box:

   <ListBox x:Name="lsbProducts" >

         Executed="DeleteProduct_Executed" />
Again, you need only to implement the CanExecute and Executed methods, as shown below:

   private void DeleteProduct_CanExecute(object sender, 
      CanExecuteRoutedEventArgs e)
      e.CanExecute = lsbProducts.SelectedItem != null;
   private void DeleteProduct_Executed(object sender, 
      ExecutedRoutedEventArgs e)
Author's Note: The CanExecute method gets called a lot—make it efficient!

Unlike the traditional approach, when using the WPF command approach, you don't need to change the command source (the Delete menu) code to add the second list box.

Also note that you don't have to consider which control has the focus in this implementation. The CommandManager (the class that coordinates WPF commands) interacts with the FocusManager to determine which control currently has the focus.

WPF Command Framework Basics
Keeping that brief example in hand, here's some basic information about how the WPF command framework works.

Command Sources
A command source, such as a button or menu, invokes commands. Usually, command sources implement ICommandSource. You can also invoke commands declaratively, through InputBindings. With an InputBinding in place, when an InputGesture (such as Ctrl-C for copy) occurs, the framework invokes the associated command (such as, ApplicationCommands.Copy).

Built-In Commands
WPF controls are hooked up to some commands/input gestures out of the box. There are many of these built-in commands in the .NET framework. The first example used the ApplicationCommands.Delete command. Other built-in commands include Copy, Paste, and many more. These commands are made even handier because many of the WPF controls are aware of them. For example, you don't have to do anything special to wire up Copy and Paste to a TextBox; the TextBox control already has the bindings built in, and automatically implements commands such as Copy and Paste.

No Automatic Implementation
However, WPF commands don't implement command behavior. This is probably the most common misconception developers have when learning about WPF commands. WPF commands merely notify UI elements that a command has been invoked—the UI elements themselves must implement any desired behavior. This separation of command from behavior is a very powerful feature, as you can see in the preceding example, where the Delete command is invoked via the same source (the Delete menu), but has two completely different implementations.

Also, WPF commands do not automatically provide undo/redo capabilities; if you need those capabilities, you still have to implement them manually within your application. However, you can connect your code to the WPF Undo and Redo commands to cause the corresponding actions to occur.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date