Menu
The Menu
control adds a top-level menu to an application. A Menu
is usually placed in a DockPanel
in a Window
, docked to the top of the window:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Open..."/>
<Separator/>
<MenuItem Header="_Exit"/>
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>
</MenuItem>
</Menu>
</DockPanel>
</Window>
A menu will usually contain a set of nested MenuItem
s. The first level of MenuItem
s represent the items that will be displayed horizontally along the menu. The second level of MenuItem
s represent the menu items that will be dropped down from the top-level and subsequent nested MenuItem
s represent sub-menus.
The text of the MenuItem
is displayed by the Header
property; the inner content of the MenuItem
is where the sub-items are placed.
Separators are added by including a Separator
control or a MenuItem
with a header of "-"
.
Accelerator Keys
An accelerator key is a key on your keyboard that you can press to quickly access a menu. It is also sometimes called a hot key, access key or mnemonic.
If you will press Alt with the example above you will see that some letters are underlined. You can use a combination of Alt + underlined letter to navigate within a menu. In Avalonia to identify the accelerator key, you will need to use _
next character to this character would be an accelerator key.
Menu Commands
Like Button
, commands can be bound to MenuItem
s. The command will be executed when the menu item is clicked or selected with the keyboard:
<Menu>
<MenuItem Header="_File">
<MenuItem Header="_Open..." Command="{Binding OpenCommand}"/>
</MenuItem>
</Menu>
See the Binding to Commands section for more information on binding to commands.
Menu Icons
A menu icon can be displayed by placing an Image
in the Icon
property:
<MenuItem Header="_Open...">
<MenuItem.Icon>
<Image Source="resm:MyApp.Assets.Open.png"/>
</MenuItem.Icon>
</MenuItem>
Checkboxes
Similarly, a CheckBox
can be displayed in the Icon
property to make the MenuItem
checkable:
<MenuItem Header="_Open...">
<MenuItem.Icon>
<CheckBox BorderThickness="0"
IsHitTestVisible="False"
Command="{Binding ToggleCommand}">
Toggle _Me
</CheckBox>
</MenuItem.Icon>
</MenuItem>