XAML Menu
This article explains how to create and use XAML Menu and Menu Items in your applications.
<Menu/> and <MenuItem /> elements represent a Menu and menu item in XAML. This article shows you to create a simple menu application using XAML.
Creating Menu and Menu Items
A Menu can have multiple menu items. The Header attribute of MenuItem represents the text of the menu item. The following code creates a menu with a sub item with text "File".
<Menu>
<MenuItem Header="File">
</MenuItem>
</Menu>
Setting Menu Properties
Similar to other XAML controls, Menus and sub menus support attributes such as height, width, background and foreground colors. The following code sets height, width, background color, and foreground color of menu and menu items.
<Menu VerticalAlignment="Top" HorizontalAlignment="Left"
Height="20" Width="50" Background="Orange" Foreground="White">
<MenuItem Header="File" Background="LightBlue"/>
</Menu>
Example
The following code creates a complete menu and menu items with different background and foreground colors.
<Window xmlns="http://schemas.microsoft.com/winfx/avalon/2005"
xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005">
<Menu VerticalAlignment="Top" HorizontalAlignment="Left"
Height="20" Width="50" Background="Orange" Foreground="White">
<MenuItem Header="File">
<MenuItem Header="New" Background="LightBlue"/>
<MenuItem Header="Open" Background="LightBlue"/>
<MenuItem Mode="Separator" /
<MenuItem Header="Database" Background="Black" Foreground="Yellow" >
<MenuItem Header="Create"/>
<MenuItem Header="Delete" />
</MenuItem>
<MenuItem Mode="Separator" />
<MenuItem Header="Exit" Background="LightBlue"/>
</MenuItem>
</Menu>
</Window>
|
The output looks like Figure 1.
Further Readings
You may also want to read these related articles.
Ask Your Question
Got a programming related question? You may want to post your question here