ContextMenu Class

Definition

Represents a pop-up menu that enables a control to expose functionality that is specific to the context of the control.

public class ContextMenu : System.Windows.Controls.Primitives.MenuBase
Inheritance
Derived
System.Windows.Controls.Ribbon.RibbonContextMenu

Inherited Members

System.Object

System.Windows.Controls.Control

System.Windows.Controls.ItemsControl

System.Windows.Controls.Primitives.MenuBase

System.Windows.DependencyObject

System.Windows.FrameworkElement

System.Windows.Media.Visual

System.Windows.Threading.DispatcherObject

System.Windows.UIElement

Examples

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

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

Remarks

ContextMenu is an ItemsControl, which means it can contain a collection of objects of any type (such as string, image, or panel). For more information, see the ItemsControl class.

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.

Customizing the ContextMenu Control

To apply the same property settings to multiple ContextMenu controls, use the Style property. You can modify the default ControlTemplate to give the control a unique appearance. For more information about creating a ControlTemplate, see Customizing the Appearance of an Existing Control by Creating a ControlTemplate. To see the parts and states that are specific to the ContextMenu, see ContextMenu Styles and Templates.

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 Default WPF Themes.

Note

Setting a visual property will only have an effect if that property is both present in ContextMenu control's default template and is set by using a . You can find a list of visual properties in the "Changing the Visual Structure of a Control" section in Customizing the Appearance of an Existing Control by Creating a ControlTemplate.

Constructors

ContextMenu()

Initializes a new instance of the ContextMenu class.

Fields

ClosedEvent

Identifies the Closed routed event.

CustomPopupPlacementCallbackProperty

Identifies the CustomPopupPlacementCallback dependency property.

HasDropShadowProperty

Identifies the HasDropShadow dependency property.

HorizontalOffsetProperty

Identifies the HorizontalOffset dependency property.

IsOpenProperty

Identifies the IsOpen dependency property.

OpenedEvent

Identifies the Opened routed event.

PlacementProperty

Identifies the Placement dependency property.

PlacementRectangleProperty

Identifies the PlacementRectangle dependency property.

PlacementTargetProperty

Identifies the PlacementTarget dependency property.

StaysOpenProperty

Identifies the StaysOpen dependency property.

VerticalOffsetProperty

Identifies the VerticalOffset dependency property.

Properties

CustomPopupPlacementCallback

Gets or sets a callback that indicates where a ContextMenu should be placed on the screen.

HandlesScrolling

Gets a value that indicates whether the control supports scrolling.

HasDropShadow

Gets or sets a value that indicates whether the context menu appears with a dropped shadow.

HorizontalOffset

Get or sets the horizontal distance between the target origin and the popup alignment point.

IsOpen

Gets or sets a value that indicates whether the ContextMenu is visible.

Placement

Gets or sets the Placement property of a ContextMenu.

PlacementRectangle

Gets or sets the area relative to which the context menu is positioned when it opens.

PlacementTarget

Gets or sets the UIElement relative to which the ContextMenu is positioned when it opens.

StaysOpen

Gets or sets a value that indicates whether the ContextMenu should close automatically.

VerticalOffset

Get or sets the vertical distance between the target origin and the popup alignment point.

Methods

OnClosed(RoutedEventArgs)

Called when the Closed event occurs.

OnCreateAutomationPeer()

Creates and returns a ContextMenuAutomationPeer object for this ContextMenu.

OnIsKeyboardFocusWithinChanged(DependencyPropertyChangedEventArgs)

Reports that the IsKeyboardFocusWithin property changed.

OnKeyDown(KeyEventArgs)

Called when a KeyDown event is raised by an object inside the ContextMenu.

OnKeyUp(KeyEventArgs)

Responds to the KeyUp event.

OnOpened(RoutedEventArgs)

Called when the Opened event occurs.

OnVisualParentChanged(DependencyObject)

Called when a context menu's visual parent changes.

PrepareContainerForItemOverride(DependencyObject, Object)

Prepares the specified element to display the specified item.

Events

Closed

Occurs when a particular instance of a ContextMenu closes.

Opened

Occurs when a particular instance of a context menu opens.