MenuItem Class
TOC
Collapse the table of content
Expand the table of content

MenuItem Class

Implements a selectable item inside a Menu.

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

'Declaration
<StyleTypedPropertyAttribute(Property:="ItemContainerStyle", StyleTargetType:=GetType(MenuItem))> _
<LocalizabilityAttribute(LocalizationCategory.Menu)> _
<TemplatePartAttribute(Name:="PART_Popup", Type:=GetType(Popup))> _
Public Class MenuItem
	Inherits HeaderedItemsControl
	Implements ICommandSource
'Usage
Dim instance As MenuItem

/** @attribute StyleTypedPropertyAttribute(Property="ItemContainerStyle", StyleTargetType=System.Windows.Controls.MenuItem) */ 
/** @attribute LocalizabilityAttribute(LocalizationCategory.Menu) */ 
/** @attribute TemplatePartAttribute(Name="PART_Popup", Type=System.Windows.Controls.Primitives.Popup) */ 
public class MenuItem extends HeaderedItemsControl implements ICommandSource
StyleTypedPropertyAttribute(Property="ItemContainerStyle", StyleTargetType=System.Windows.Controls.MenuItem) 
LocalizabilityAttribute(LocalizationCategory.Menu) 
TemplatePartAttribute(Name="PART_Popup", Type=System.Windows.Controls.Primitives.Popup) 
public class MenuItem extends HeaderedItemsControl implements ICommandSource
<MenuItem>
  Items
</MenuItem>

ContentModel: A MenuItem is a HeaderedItemsControl. For more information on the content model for MenuItem, see HeaderedItemsControl Content Model.

A MenuItem can be

  • Selected to invoke commands.

  • Separators.

  • Headers for submenus.

  • Checked or unchecked.

NoteNote:

 By default, the HorizontalAlignment of a MenuItem is set to Stretch. The default horizontal position of a StackPanel is Center. If you set the Width property of a MenuItem through a StackPanel, the stack panel's default is applied and the item will be centered.

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.

<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>

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.

More Code

How to: Create a ContextMenu

This example describes how to create a ContextMenu.

How to: Create a Control That Has an Access Key and Text Wrapping

This example shows how to create a control that has an access key and supports text wrapping. The example uses a Label control to illustrate these concepts.

System.Object
   System.Windows.Threading.DispatcherObject
     System.Windows.DependencyObject
       System.Windows.Media.Visual
         System.Windows.UIElement
           System.Windows.FrameworkElement
             System.Windows.Controls.Control
               System.Windows.Controls.ItemsControl
                 System.Windows.Controls.HeaderedItemsControl
                  System.Windows.Controls.MenuItem

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2016 Microsoft