Table of Contents
Creating Static Menu

The following XAML demonstrates how to define a static menu items with an EO.Wpf Menu control:

XAML
<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:eo="http://schemas.essentialobjects.com/wpf/"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <eo:Menu IsMainMenu="True">
            <eo:MenuItem Header="_File" />
            <eo:MenuItem Header="_Edit" />
            <eo:MenuItem Header="_View" />
            <eo:MenuItem Header="_Window" />
            <eo:MenuItem Header="_Help" />
        </eo:Menu>
    </StackPanel>
</Window>

The above code produces the following result:

The above code also demonstrates the following features:

  • Use "_" to add an underscore to the menu item text. For example, "_File" underscores letter "F". Note the underscore is only displayed when the menu is active;
  • IsMainMenu
  • is set to true. This activates the menu if the user presses F10 or the ALT key.

Each menu item can have sub menu items. The following XAML defines two sub menu items for the "File" menu item:

XAML
<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:eo="http://schemas.essentialobjects.com/wpf/"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <eo:Menu IsMainMenu="True">
            <eo:MenuItem Header="_File">
                <eo:MenuItem Header="_New" Click="File_New_Click" />
                <eo:MenuItem Header="_Close" Click="File_Exit_Click" />
            </eo:MenuItem>
            <eo:MenuItem Header="_Edit" />
            <eo:MenuItem Header="_View" />
            <eo:MenuItem Header="_Window" />
            <eo:MenuItem Header="_Help" />
        </eo:Menu>
    </StackPanel>
</Window>

The above code produces the following result:

The above code also demonstrates how to handle menu item click event. For example, the File_Close event handler can contain the following code to close the window when the item is clicked:

private void File_Exit_Click(object sender, RoutedEventArgs e)
{
    //Close the window
    Close();
}

Alternately, you can also use Command property to bind the menu item to a command. The following code also demonstrates how to add a keyboard shortcut to the menu item:

XAML
<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:eo="http://schemas.essentialobjects.com/wpf/"
        Title="MainWindow" Height="350" Width="525">
    <Window.InputBindings>
        <KeyBinding Key="X" Modifiers="Alt" Command="ApplicationCommands.Close" />
    </Window.InputBindings>
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Close" Executed="File_Exit_Click"></CommandBinding>
    </Window.CommandBindings>
    <StackPanel>
        <eo:Menu x:Name="Menu1" IsMainMenu="True">
            <eo:MenuItem Header="_File">
                <eo:MenuItem Header="_New" />
                <eo:MenuItem IsSeparator="True"></eo:MenuItem>
                <eo:MenuItem Header="Close" Command="ApplicationCommands.Close" InputGestureText="Alt+X"></eo:MenuItem>
            </eo:MenuItem>
            <eo:MenuItem Header="_Edit" />
            <eo:MenuItem Header="_View" />
            <eo:MenuItem Header="_Window" />
            <eo:MenuItem Header="_Help" />
        </eo:Menu>
    </StackPanel>
</Window>

The above code produces the following result:

The above sample also demonstrated how to create a separator by setting a MenuItem's IsSeparator to true.