Table of Contents

Menu

AlterNET UI allows building menus on all the platforms it supports; see the screenshots below.

The Menu and MainMenu classes enable you to organize elements associated with commands and event handlers in a hierarchical order. Each Menu element contains a collection of MenuItem elements.

The Menu control presents a list of items that specify commands or options for an application. Typically, clicking a MenuItem opens a submenu or causes an application to execute a command.

Creating Menus

The following example creates a MainMenu with Menu items inside. The Menu contains MenuItem objects that use the Command, Text, Checked properties and the Click event.

<Window.Menu>
  <MainMenu>
    <MenuItem Text="_File">
      <MenuItem Text="_Open..." Name="openMenuItem" Click="OpenMenuItem_Click" Shortcut="Ctrl+O"/>
      <MenuItem Text="_Save..." Name="saveMenuItem" Command="{Binding SaveCommand}"/>
      <MenuItem Text="-" Name="separatorMenuItem" />
      <MenuItem Text="E_xit" Name="exitMenuItem" Click="ExitMenuItem_Click" />
    </MenuItem>
    <MenuItem Text="_View">
      <MenuItem Text="_Grid" Checked="True" Name="gridMenuItem" Click="GridMenuItem_Click"/>
    </MenuItem>
  </MainMenu>
</Window.Menu>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;
        SaveCommand = new Command(o => MessageBox.Show("Save"));
    }

    public Command SaveCommand { get; }

    private void OpenMenuItem_Click(object sender, EventArgs e) => MessageBox.Show("Open");

    private void GridMenuItem_Click(object sender, EventArgs e) => MessageBox.Show("Grid item is checked: " + gridMenuItem.Checked);

    private void ExitMenuItem_Click(object sender, EventArgs e) => Close();
}

Keyboard shortcuts are character combinations that can be entered with the keyboard to invoke Menu commands. For example, the shortcut for Copy is CTRL+C. To assign a keyboard shortcut to a menu item, use the Shortcut property.

On macOS, by the system UI guidelines, the About, Quit, and Preferences items must be placed into the application (the leftmost) menu. They also should have standard names and keyboard shortcuts. On Windows and Linux, these items are usually located in different menus, like Help, File, and Tools. As many applications include these menu items, AlterNET UI provides automatic role-based menu item location adjustment on macOS. Usually, the developer does not have to do anything, as the framework automatically deduces item roles from the menu item names and relocates them to the required menu on macOS. For cases when more fine control is required, please use Role property, and MenuItemRoles class.