BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Windows Presentation Foundation
.NET 4.0+

WPF Menu Controls - MenuItem

The seventy-seventh part of the Windows Presentation Foundation Fundamentals tutorial continues to describe the WPF menu controls, which present the user with a number of commands from which they may select. The second menu control is MenuItem.

Access Keys

To make it easier to use menus without a pointing device, you can assign access keys to MenuItems. An access key is usually a modified character from the text in the MenuItem's Header property. If the user activates the menu by pressing the Alt key, the access key character appears underlined, indicating to the user the key than they can press to execute the command or open the submenu.

Access keys are defined in the same manner as for the Label control, which we saw in an earlier article. In most cases, adding an underscore to the text before the desired character defines the access key. The underscore does not appear in the menu as a separate symbol. For more details on the syntax for access keys, refer to the WPF Label article.

To add access keys to the menu, replace the XAML for the window with the code below. Note the positions of the underscores.

<Window x:Class="MenuItemDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Not Notepad"
        Height="250"
        Width="350">
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <MenuItem Header="_New" Click="New_Click"/>
                <MenuItem Header="_Open..."/>
                <MenuItem Header="_Save"/>
                <MenuItem Header="Save _As"/>
                <Separator />
                <MenuItem Header="Page Set_up..."/>
                <MenuItem Header="_Print..."/>
                <Separator />
                <MenuItem Header="E_xit"/>
            </MenuItem>
            <MenuItem Header="_Edit">
                <MenuItem Header="_Undo"/>
                <Separator />
                <MenuItem Header="Cu_t"/>
                <MenuItem Header="_Copy"/>
                <MenuItem Header="_Paste"/>
                <MenuItem Header="De_lete"/>
                <Separator />
                <MenuItem Header="_Find..."/>
                <MenuItem Header="Find _Next"/>
                <MenuItem Header="_Replace..."/>
                <MenuItem Header="_Go To..."/>
                <Separator />
                <MenuItem Header="_Select">
                    <MenuItem Header="_All"/>
                    <MenuItem Header="_Paragraph"/>
                </MenuItem>
                <MenuItem Header="Time/Date"/>
            </MenuItem>
            <MenuItem Header="F_ormat">
                <MenuItem Header="_Word Wrap"/>
                <MenuItem Header="_Font..."/>
                <MenuItem Header="_Colour"/>
            </MenuItem>
            <MenuItem Header="_View">
                <MenuItem Header="_Status Bar"/>
            </MenuItem>
            <MenuItem Header="_Help">
                <MenuItem Header="View _Help"/>
                <Separator />
                <MenuItem Header="_About Not Notepad"/>
            </MenuItem>
        </Menu>
        <TextBox Name="MyText" AcceptsReturn="True" AcceptsTab="True" TextWrapping="NoWrap" />
    </DockPanel>
</Window>

You can see that the "New" menu now includes a Click event. We'll use this to show the use of the access keys. To do so, let's make the menu remove the text from the TextBox. Add the following code behind the window:

private void New_Click(object sender, RoutedEventArgs e)
{
    MyText.Clear();
}

Run the program to see the results. Start by adding some text to the TextBox. Press Alt to activate the menu bar and you should see the access keys underlined. Press F to open the File menu and N to select the New command. The text box will be emptied.

WPF MenuItems with access keys

8 July 2014