ContextMenu Class
Updated: September 2010
Represents a pop-up menu that enables a control to expose functionality that is specific to the context of the control.
Assembly: PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Content Model: ContextMenu is a ItemsControl. Its content properties are Items and ItemsSource. For more information on the content model for ContextMenu, see Controls Content Model Overview.
The properties of the ContextMenu class are used to define the position and behavior of the ContextMenu.
A FrameworkElement or FrameworkContentElement can have a ContextMenu. If you assign a ContextMenu to the FrameworkElement.ContextMenu or FrameworkContentElement.ContextMenu property, the ContextMenuService class handles context menu operations in response to user interaction. Part of the work the ContextMenuService does is opening the ContextMenu when the user clicks the right mouse button over the control.
The following properties are defined by the ContextMenu and ContextMenuService classes. If any of these properties are set on both ContextMenu and the ContextMenuService, the property value from the ContextMenuService is used.
A ContextMenu is automatically placed inside a Popup control. This behavior cannot be changed.
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.
The following example creates a ContextMenu that manipulates the text of a TextBox. The example demonstrates how to subscribe to and handle the events of MenuItem objects in a ContextMenu.
<TextBox Name="textBox1" TextWrapping="Wrap" Margin="10, 10, 5, 5" Grid.Row="7"> The quick brown fox jumps over the lazy dog. <TextBox.ContextMenu> <ContextMenu> <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" /> </ContextMenu> </TextBox.ContextMenu> </TextBox>
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
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.Primitives.MenuBase
System.Windows.Controls.ContextMenu
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.