A Brief Look at Menus in WPF - Handling Input
(Page 3 of 4 )
When a user clicks a menu item, something will need to be done in response. The Click event is raised when a menu item is clicked, so this is the obvious place to give the user a response. The Click attribute can be used to point to an appropriate event handler:
<MenuItem Header="Quit" Click="MenuItem_Click" />
The actual event handler would look something like this:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
// Do something
}
But recall from earlier that it's possible to make a menu item checkable. If this is the case, then it's a good idea to respond when the user has checked the menu item and when the user has unchecked the menu item. When the user checks an item, a Checked event is raised, and when the user unchecks an item, an Unchecked event is raised:
<MenuItem Header="Syntax Highlighting" IsCheckable="True"
Checked="MenuItem_Checked"
Unchecked="MenuItem_Unchecked" />
private void MenuItem_Checked(object sender, RoutedEventArgs e)
{
// The user has checked the item
}
private void MenuItem_Unchecked(object sender, RoutedEventArgs e)
{
// The user has unchecked the item
}
Working with the above three events allows for a consistent response each time. However, sometimes, the effect of a menu item changes depending on what the user is actually doing. For example, consider the Edit menu. The use can copy, cut and paste text using this menu. However, this is only appropriate with text boxes, and the exact targets may vary. Furthermore, the user can copy, cut and paste in other ways as well, so the menu shouldn't be the only originator of these events.
This is where commands come into play. Commands allow for various sources to originate a single action, and for the target to handle the action in its own way. A user may generate a Paste command through the main menu or through a keyboard shortcut, but the same command is generated both times. The same command can have different effects, however, depending on the target. One text box may allow formatting in the pasted text, and another text box may strip all formatting away.
Linking menu options to commands is very easy. The Command property of a MenuItem simply needs to be set to the desired command (represented by an ICommand). For example, say we wanted to link Copy, Cut and Paste events to menu items. These are fairly common commands, and their functionality is already set up for us (it's of course possible to create your own commands, but this article can't possibly cover all of that; we'll stick to the relationship between commands and menus). We'd set it all up like this:
<MenuItem Header="Copy"
Command="ApplicationCommands.Copy"/>
<MenuItem Header="Cut"
Command="ApplicationCommands.Cut" />
<MenuItem Header="Paste"
Command="ApplicationCommands.Paste" />
Not only does it link the menu item with the command, but it also disables the menu items where appropriate (such as when there's nothing to copy, cut or paste), and it displays the appropriate keyboard shortcuts beside each item. In fact, we can even remove the headers if we're fine with the standard ones-they will automatically be set:
<MenuItem Command="ApplicationCommands.Copy"/>
<MenuItem Command="ApplicationCommands.Cut" />
<MenuItem Command="ApplicationCommands.Paste" />
Next: Context Menus >>
More Windows Scripting Articles
More By Peyton McCullough