Enter WPF Commands
Here's how you could hook up the first example (the single list box window) with WPF commands.
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
. Here are their implementations:
private void DeleteCustomer_CanExecute(
object sender, CanExecuteRoutedEventArgs e)
e.CanExecute = lsbCustomers.SelectedItem != null;
private void DeleteCustomer_Executed(object sender,
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" >
Again, you need only to implement the CanExecute
methods, as shown below:
private void DeleteProduct_CanExecute(object sender,
e.CanExecute = lsbProducts.SelectedItem != null;
private void DeleteProduct_Executed(object sender,
|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.
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
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
, 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
commands to cause the corresponding actions to occur.