Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
MenuItem Class
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
MenuItem Class

Updated: November 2007

Represents a selectable item inside a Menu.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/xaml/presentation

Visual Basic (Declaration)
<StyleTypedPropertyAttribute(Property := "ItemContainerStyle", StyleTargetType := GetType(MenuItem))> _
<LocalizabilityAttribute(LocalizationCategory.Menu)> _
<TemplatePartAttribute(Name := "PART_Popup", Type := GetType(Popup))> _
Public Class MenuItem _
    Inherits HeaderedItemsControl _
    Implements ICommandSource
Visual Basic (Usage)
Dim instance As MenuItem
C#
[StyleTypedPropertyAttribute(Property = "ItemContainerStyle", StyleTargetType = typeof(MenuItem))]
[LocalizabilityAttribute(LocalizationCategory.Menu)]
[TemplatePartAttribute(Name = "PART_Popup", Type = typeof(Popup))]
public class MenuItem : HeaderedItemsControl, ICommandSource
Visual C++
[StyleTypedPropertyAttribute(Property = L"ItemContainerStyle", StyleTargetType = typeof(MenuItem))]
[LocalizabilityAttribute(LocalizationCategory::Menu)]
[TemplatePartAttribute(Name = L"PART_Popup", Type = typeof(Popup))]
public ref class MenuItem : public HeaderedItemsControl, 
    ICommandSource
J#
/** @attribute StyleTypedPropertyAttribute(Property = "ItemContainerStyle", StyleTargetType = MenuItem) */
/** @attribute LocalizabilityAttribute(LocalizationCategory.Menu) */
/** @attribute TemplatePartAttribute(Name = "PART_Popup", Type = Popup) */
public class MenuItem extends HeaderedItemsControl implements ICommandSource
JScript
public class MenuItem extends HeaderedItemsControl implements ICommandSource
XAML Object Element Usage
<MenuItem>
  Items
</MenuItem>

Content Model: MenuItem is a HeaderedItemsControl. Its content properties are Items and ItemsSource and its header property is Header. For more information on the content model for MenuItem, see Controls Content Model Overview.

A MenuItem can have submenus. The submenu of the MenuItem is made up of the objects within the ItemCollection of a MenuItem. It is common for a MenuItem to contain other MenuItem objects to create nested submenus.

A MenuItem can have one of several functions:

  • It can be selected to invoke a command.

  • It can be a separator for other menu items.

  • It can be a header for a submenu.

  • It can be checked or unchecked.

Note:

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

The MenuItem handles the MouseDown event, so if you attach an event handler to MouseDown, your handler will never be called. To add your own handler, subscribe to the PreviewMouseDown event or subscribe to MouseDown by calling AddHandler(RoutedEvent, Delegate, Boolean) with handledEventsToo set to true.

The following example creates a Menu to manipulate text in a TextBox. The Menu contains MenuItem objects that use the Command, IsCheckable, and Header properties and the Checked, Unchecked, and Click events.

C#
<Menu>
  <MenuItem Header="_Edit">
    <MenuItem Command="ApplicationCommands.Copy"/>
    <MenuItem Command="ApplicationCommands.Cut"/>
    <MenuItem Command="ApplicationCommands.Paste"/>
  </MenuItem>
  <MenuItem Header="_Font">
    <MenuItem Header="_Bold" IsCheckable="True"
              Checked="Bold_Checked"
              Unchecked="Bold_Unchecked"/>
    <MenuItem Header="_Italic" IsCheckable="True"
              Checked="Italic_Checked"
              Unchecked="Italic_Unchecked"/>
    <Separator/>
    <MenuItem Header="I_ncrease Font Size"
              Click="IncreaseFont_Click"/>
    <MenuItem Header="_Decrease Font Size"
              Click="DecreaseFont_Click"/>
  </MenuItem>
</Menu>
<TextBox Name="textBox1" TextWrapping="Wrap"
         Margin="2">
  The quick brown fox jumps over the lazy dog.
</TextBox>

Visual Basic
Private Sub Bold_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    textBox1.FontWeight = FontWeights.Bold
End Sub

Private Sub Bold_Unchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    textBox1.FontWeight = FontWeights.Normal
End Sub

Private Sub Italic_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    textBox1.FontStyle = FontStyles.Italic
End Sub

Private Sub Italic_Unchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    textBox1.FontStyle = FontStyles.Normal
End Sub

Private Sub IncreaseFont_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    If textBox1.FontSize < 18 Then
        textBox1.FontSize += 2
    End If
End Sub

Private Sub DecreaseFont_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    If textBox1.FontSize > 10 Then
        textBox1.FontSize -= 2
    End If
End Sub

C#
private void Bold_Checked(object sender, RoutedEventArgs e)
{
    textBox1.FontWeight = FontWeights.Bold;
}

private void Bold_Unchecked(object sender, RoutedEventArgs e)
{
    textBox1.FontWeight = FontWeights.Normal;
}

private void Italic_Checked(object sender, RoutedEventArgs e)
{
    textBox1.FontStyle = FontStyles.Italic;
}

private void Italic_Unchecked(object sender, RoutedEventArgs e)
{
    textBox1.FontStyle = FontStyles.Normal;
}

private void IncreaseFont_Click(object sender, RoutedEventArgs e)
{
    if (textBox1.FontSize < 18)
    {
        textBox1.FontSize += 2;
    }
}

private void DecreaseFont_Click(object sender, RoutedEventArgs e)
{
    if (textBox1.FontSize > 10)
    {
        textBox1.FontSize -= 2;
    }
}

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 Vista

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker