Freigeben über


How to: Create a Menu

This example shows how to create Menu controls. The example illustrates Menu controls with submenus and MenuItem elements with ToolTip controls. The example also shows how to use the IsCheckable property to make MenuItem controls that can be checked.

Example

<Menu Width="30" Margin="10, 10, 5, 5" HorizontalAlignment="Left" Background="White">
   <MenuItem Header="_File">
          <MenuItem Header="_New" IsCheckable="true"/>
          <MenuItem Header="_Open" IsCheckable="true"/>
          <MenuItem Header="_Close" IsCheckable="true"/>
      <Separator/>
          <MenuItem Header="Menu with Submenu">
                <MenuItem Header="_submenuitem1" IsCheckable="true"/>
                <MenuItem Header="_submenuitem2" IsCheckable="true">          
                    <MenuItem Header="_submenuitem2.1" IsCheckable="true"/>
                </MenuItem>
         </MenuItem>
         <Separator/>
         <MenuItem Header="_Menu item with ToolTip">
          <MenuItem.ToolTip>
            <ToolTip>
            ToolTip Information.
            </ToolTip>
          </MenuItem.ToolTip>
      </MenuItem>
   </MenuItem>
</Menu>
menu = new Menu();
menu.Background = Brushes.LightBlue;
mi = new MenuItem();
mi.Width = 50;
mi.Header = "_File";
menu.Items.Add(mi);

mia = new MenuItem();
mia.Header = "_Cut";
mia.InputGestureText = "Ctrl+X";
mi.Items.Add(mia);

mib = new MenuItem();
mib.Command = System.Windows.Input.ApplicationCommands.Copy;
mib.Header = "_Copy";
mi.Items.Add(mib);

mic = new MenuItem();
mic.Command = System.Windows.Input.ApplicationCommands.Paste;
mic.Header = "_Paste";
mi.Items.Add(mic);
cv2.Children.Add(menu);
Sub OnClick(ByVal sender As Object, ByVal args As RoutedEventArgs)
   Dim mn As New Menu()
   mn.Background = Brushes.LightBlue
   
   Dim mi As New MenuItem()
   mi.Header = ("_File")

   Dim mi1 As New MenuItem()
   mi1.Header = ("_Cut")
   mi1.InputGestureText = "Ctrl+X"

   Dim mi2 As New MenuItem()
   mi2.Command = System.Windows.Input.ApplicationCommands.Copy
   mi2.Header = "_Copy"

   Dim mi3 As New MenuItem()
   mi3.Command = System.Windows.Input.ApplicationCommands.Paste
   mi3.Header = "_Paste"

   mn.Items.Add(mi)
   mi.Items.Add(mi1)
   mi.Items.Add(mi2)
   mi.Items.Add(mi3)

  cv2.Children.Add(mn)

End Sub

For the complete sample, see Menu Sample.

See Also

Tasks

How to: Create a Menu with Keyboard Shortcuts

Reference

Menu
MenuItem

Concepts

Menu Overview