Export (0) Print
Expand All

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

[StyleTypedPropertyAttribute(Property="ItemContainerStyle", StyleTargetType=typeof(MenuItem))] 
[LocalizabilityAttribute(LocalizationCategory.Menu)] 
[TemplatePartAttribute(Name="PART_Popup", Type=typeof(Popup))] 
public class MenuItem : HeaderedItemsControl, ICommandSource
/** @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>

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

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:
© 2014 Microsoft