In WPF, the Menu and the MenuItem classes represent a menu and a menu item respectively. A Menu is a collection of menu items with a command associated with each menu item. A menu item may have children menu items called submenus. This article discusses how to work with menus in WPF applications using XAML and C#.
Creating a WPF Menu at Design Time
The Menu element in XAML creates a menu control.
The Name property defines the name of the menu and Height and Width represents the height and width of a menu control.
- <Menu Name="menu1" Height="22" Width="200" />
To position a menu control in a Window, the Margin, HorizontalAlignment and VerticalAlignment properties may be used. The following code sets horizontal, vertical alignments, margin, and background color of a menu control.
- <Menu Height="22" Name="menu1" Width="200" Margin="10, 10, 5, 5" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Chocolate">
- </Menu>
Setting WPF Menu Control Properties
There are three ways to set menu control properties. You may use the Properties windows, set properties in XAML manually, or set properties at run-time using WPF code.
If you right click on the menu control and select Properties menu item, you will see the Properties window same as Figure 1.
Figure 1. Properties Window
As you can see from Figure 1, you can set all properties of a Menu control through this Window such as border thickness, opacity, bitmap effects, background and foreground colors, alignments, width and height, layouts, and fonts.
Once you set properties in the Properties window, the respective XAML code is written in the XAML file by the designer. For example, I set BorderThickness to 2, BitmapEffect to DropShadowEffect, and Background to Blue as shown in Figure 2.
Figure 2. Setting a Menu Control Properties
If you look at the XAML file now, you will see Menu code like below where you can see BorderThickness is set to 2, Background is set to Blue, and Menu.BitmapEffect element is added within the Menu element.
- <Menu Height="22" Name="menu1" Width="200" Margin="10, 10, 5, 5" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Blue" BorderThickness="2">
- <Menu.BitmapEffect>
- <DropShadowBitmapEffect />
- </Menu.BitmapEffect>
- </Menu>
Now, the menu looks like Figure 3.
Figure 3. Menu with blue background and drop shadow effect
Adding WPF Menu Items
Now let's add menu items and sub menus to the menu control. The MenuItem element adds a menu item to the menu control. The following code shows the initial syntax of the MenuItem element. The Header attribute is the name of the MenuItem.
- <MenuItem Header="Menu Item Name " />
A MenuItem can have other MenuItem elements within it as child/sub menus and can go up to several levels. The following code adds three children menu items to first menu item.
- <MenuItem Header="_File">
- <MenuItem Header="_Open" IsCheckable="true" />
- <MenuItem Header="_Close" IsCheckable="true" />
- <MenuItem Header="_Save" IsCheckable="true" />
- </MenuItem>
The output looks like Figure 4.
Figure 4. A menu with menu items
A separate is used to separate categories of menu items. We can add a separator to a menu control by using <Separator /> element.
We can also add sub menus and sub menu items using the MenuItem element within parent a MenuItem element. The following code adds a separator and sub menus and sub menu items to the menu.
- <Separator />
- <MenuItem Header="Sub Items">
- <MenuItem Header="Child1 SubItem" IsCheckable="true" />
- <MenuItem Header="Child2 SubItem" IsCheckable="true">
- <MenuItem Header="GrandChild2 SubItem" IsCheckable="true" />
- </MenuItem>
- </MenuItem>
Now, our new output looks like Figure 5.

Figure 5. A menu with menu items
Adding Tooltips to a WPF Menu
The MenuItem.ToolTip element adds a tooltip to a menu item. The following code adds a tooltip to the Open menu item.
- <MenuItem Header="_Open" IsCheckable="true">
- <MenuItem.ToolTip>
- <ToolTip> Open a file. </ToolTip>
- </MenuItem.ToolTip>
- </MenuItem>
The output with the tooltip looks like Figure 6.
Figure 6.A menu with a tooltip
Adding a CheckBox to a Menu Item
By setting IsCheckable property of a MenuItem to true makes a menu item a CheckBox in front of the header text.
- <MenuItem IsCheckable="true">

Figure 7
Adding a Keyboard Shortcut to a Menu Item
InputGestureText property is used to add keyboard shortcut to the menu item. The following code adds CTRL+O to a menu item.
- <MenuItem IsCheckable="true" Header="_Open" InputGestureText="Ctrl+O">
You can also add ALT support to a menu item by adding underscore (_) before a character and ALT+character will work. For example, in the previous line of code, _Open menu item can be executed using ALT+O.
Adding an Event Trigger to a MenuItem
The Click event is used to add the menu item click event handler. The following code adds a click event handler for a menu item.



Join the conversation! Your thoughts help the community grow.