Using XAML Menus in WPF

XAML Menus in WPF

XAML Menu and MenuItem elements represent menus in WPF. A Menu is a collection of menu items with a command associated with each menu items. A menu item may have children menu items called submenus. This article discusses how to work with menus in XAML and WPF applications.

Creating a Menu as Design Time

The Menu tag 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.

  1. <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 the horizontal and vertical alignments, the margin and the background color of a menu control.
  1. <Menu Height="22" Name="menu1" Width="200" Margin="10, 10, 5, 5" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Chocolate">  
  2. </Menu>  
Setting 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 the Properties menu item, you will see the Properties window as in Figure 1.


Figure 1. Properties Window

As you can see from Figure 1, you can set all the properties of a Menu control using this Window such as border thickness, opacity, bitmap effects, background and foreground colors, alignments, width and height, layouts and fonts.

Once you set the 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 the Menu code like the following where you can see BorderThickness is set to 2, Background is set to Blue and the Menu.BitmapEffect tag is added within the Menu tag.
  1. <Menu Height="22" Name="menu1" Width="200" Margin="10, 10, 5, 5" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Blue" BorderThickness="2">  
  2.       <Menu.BitmapEffect>  
  3.           <DropShadowBitmapEffect />  
  4.       </Menu.BitmapEffect>  
  5. </Menu>  
Now, the menu looks as in Figure 3.


Figure 3. Menu with blue background and drop shadow effect


Adding Menu Items and Sub Menus to a Menu

Now let's add menu items and sub menus to the menu control. The MenuItem tag adds a menu item to the menu control. The following code shows the initial syntax of the MenuItem tag.

The Header attribute is the name of the MenuItem.
  1. <MenuItem Header="Menu Item Name " />       
A MenuItem can have other MenuItem tags within it as child/sub menus for several levels. The following code adds three children menu items to the first menu item.
  1. <MenuItem Header="_File">             
  2.        <MenuItem Header="_Open" IsCheckable="true"/>  
  3.        <MenuItem Header="_Close" IsCheckable="true"/>  
  4.        <MenuItem Header="_Save" IsCheckable="true"/>  
  5. </MenuItem>    
The output looks as in Figure 4.


Figure 4. A menu with menu items

A separator is used to separate categories of menu items. We can add a separator to a menu control using the <Separator /> tag.

We can also add sub menus and sub menu items using the MenuItem tag within the parent, a MenuItem tag. The following code adds a separator and sub menus and sub menu items to the menu.
  1. <Separator/>  
  2. <MenuItem Header="Sub Items">  
  3.     <MenuItem Header="Child1 SubItem" IsCheckable="true"/>  
  4.     <MenuItem Header="Child2 SubItem" IsCheckable="true">  
  5.         <MenuItem Header="GrandChild2 SubItem" IsCheckable="true"/>  
  6.      </MenuItem>  
  7. </MenuItem>  
Now, our new output looks as in Figure 5.


Figure 5. A menu with menu items

Adding Tooltips to Menus

The MenuItem.ToolTip tag adds a tooltip to a menu item. The following code adds a tooltip to the Open menu item.
  1. <MenuItem Header="_Open" IsCheckable="true">  
  2.     <MenuItem.ToolTip>  
  3.         <ToolTip>  
  4.              Open a file.   
  5.         </ToolTip>  
  6.     </MenuItem.ToolTip>  
  7. </MenuItem>  
The output with the tooltip looks as in Figure 6.


Figure 6. A menu with a tooltip


Adding a CheckBox to a Menu Item

Setting the IsCheckable property of a MenuItem to true makes a menu item a CheckBox in front of the header text.
  1. <MenuItem IsCheckable="true">  


Adding a Keyboard Shortcut to a Menu Item


The InputGestureText property is used to add keyboard shortcuts to the menu item. The following code adds CTRL+O to a menu item.
  1. <MenuItem IsCheckable="true" Header="_Open" InputGestureText="Ctrl+O">  
Adding an Event Trigger to a MenuItem

The Click event adds the menu item click event handler. The following code adds a click event handler for a menu item.
  1. <MenuItem IsCheckable="true" Header="_Open" Click="MenuItem_Click">  
The event handler is defined as  in the following in the code behind. I added a message box when the menu item is clicked.
  1. private void MenuItem_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     MessageBox.Show("Menu item clicked");  
  4. }   
Creating a Menu Control at Run-time

The following code creates a menu and adds menu items dynamically.
  1. Menu mainMenu = new Menu();  
  2. mainMenu.Background = Brushes.LightGreen;  
  3. mainMenu.Height = 300;  
  4. mainMenu.Width = 200;  
  5.   
  6. MenuItem item1 = new MenuItem();  
  7. item1.Width = 50;  
  8. item1.Header = "First";  
  9. mainMenu.Items.Add(item1);  
  10.   
  11. MenuItem item2 = new MenuItem();  
  12. item2.Width = 50;  
  13. item2.Header = "Two";  
  14. item1.Items.Add(item2);  
  15.   
  16. MenuItem item3 = new MenuItem();  
  17. item3.Width = 50;  
  18. item3.Header = "Third";  
  19. item1.Items.Add(item3);  
Summary

In this article, I discussed how to use a Menu and MenuItem controls to create menus in a WPF application. We also saw how to set menu properties, add menu items at design-time as well as at run-time and add menu item event handlers.

Up Next
    Ebook Download
    View all
    Learn
    View all