Updated: February 2009
Represents a selectable item inside a Menu.
Assembly: PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
[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
Content Model: is a HeaderedItemsControl. Its content properties are Items and ItemsSource and its header property is Header. For more information on the content model for , see Controls Content Model Overview.
A can have submenus. The submenu of the is made up of the objects within the ItemCollection of a . It is common for a to contain other objects to create nested submenus.
A 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.
By default, the HorizontalAlignment of a is set to HorizontalAlignment::Stretch. The default horizontal position of a StackPanel is HorizontalAlignment::Center. If you set the Width property of a through a StackPanel, the stack panel's default is applied and the item is centered.
The 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.
Dependency properties for this control might be set by the control’s default style. If a property is set by a default style, the property might change from its default value when the control appears in the application. The default style is determined by which desktop theme is used when the application is running. For more information, see Themes.
<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>
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
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.