Перейти к основному содержимому
Версия: 11.0.0


The menu control can add menu structure to an application. You will usually place a menu at the top edge of a dock panel control, so that it is drawn at the top of a window.

к сведению

For reference information about the dock panel, see here.

A menu element will usually contain a set of nested <MenuItem> elements. The first level of menu items defines the horizontal part of the menu. Subsequent levels of menu items are drop-downs.

The caption of a menu item is set by the Header property. The content zone of a menu item can contain sub-items if required.

You can add menu separator lines by including a <Separator> element or by adding a menu item with its header set to the minus sign, like this:

<MenuItem Header="-" />

Useful Properties

You will probably use these properties most often:

MenuDockPanel.DockPosition the menu on the top edge of a dock panel.
MenuItemHeaderThe menu item caption.
MenuItemCommandThe command to be executed when the menu item is clicked or selected with the keyboard.
MenuItemMenuItem.IconContains an icon graphic to display alongside the menu item.
SeparatorA menu item separator line.


This example creates a menu docked at the top edge of a window.

<Window ...>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Open..."/>
<MenuItem Header="_Exit"/>
<MenuItem Header="_Edit">
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>

Accelerator Keys

An accelerator key is identified by a single letter in the header preceded by an underscore. For example:

 <MenuItem Header="_File">

It allows the user to access a menu item quickly. It is also sometimes called a hot key, access key or mnemonic.

The user can access this feature by first pressing the Alt key, and then the accelerator key (or they can be pressed together). This is demonstrated in the second of the menu sequences in the example above.

You will see that accelerator keys, where defined, are underlined on the menu as soon as the Alt key is pressed. Then any sub-menus are dropped down as soon as the accelerator key above is pressed.

Once keyboard interaction has been initiated with the Alt key, the user can also navigate the menus using the keyboard arrow keys. Menu items may be selected using the Enter key on the keyboard.

To initiate an action, the command property of a menu item can be bound to an ICommand object. The command will be executed when the menu item is clicked or selected with the keyboard. For example:

<MenuItem Header="_File">
<MenuItem Header="_Open..." Command="{Binding OpenCommand}"/>
к сведению

For guidance on how to bind to commands, see here.

A menu icon can be displayed by placing an image or a path icon in the <MenuItem.Icon> attached property. For example:

<MenuItem Header="_Edit">
<MenuItem Header="Copy">
<PathIcon Data="{StaticResource copy_regular}"/>
<MenuItem Header="Paste">
<PathIcon Data="{StaticResource clipboard_paste_regular}"/>
к сведению

For more detailed guidance on how to add icons to your menus, see here.

More Information

к сведению

For the complete API documentation about this control, see here.

к сведению

View the source code on GitHub Menu.cs