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.

MenuItem

In the previous article we looked at the Menu control, which produces a menu bar to act as a container for a hierarchical set of commands and, optionally, some logical grouping using Separators. The individual commands are provided by MenuItem controls. We touched upon MenuItem in the earlier article in order to demonstrate the Menu container. In this article we'll expand upon MenuItem by looking at some of its additional properties.

To demonstrate, we'll need a sample solution. Create a new WPF application project in Visual Studio, naming it, "MenuItemDemo". Once prepared, replace the XAML in the main window with the code shown below:

<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"/>
                <MenuItem Header="Open..."/>
                <MenuItem Header="Save"/>
                <MenuItem Header="Save As"/>
                <Separator />
                <MenuItem Header="Page Setup..."/>
                <MenuItem Header="Print..."/>
                <Separator />
                <MenuItem Header="Exit"/>
            </MenuItem>
            <MenuItem Header="Edit">
                <MenuItem Header="Undo"/>
                <Separator />
                <MenuItem Header="Cut"/>
                <MenuItem Header="Copy"/>
                <MenuItem Header="Paste"/>
                <MenuItem Header="Delete"/>
                <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="Format">
                <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>

The XAML creates a window with a menu bar and a TextBox, with menus that are similar to those of the standard Notepad utility. I've added a few extra menu items to show the hierarchical arrangement and for the samples in this article.

Run the program to see the layout of the menus, the hierarchy of the commands and the use of separators.

WPF MenuItem demonstration window

8 July 2014