ContextMenuService Class

 

Provides the system implementation for displaying a ContextMenu.

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

System.Object
  System.Windows.Controls.ContextMenuService

[<AbstractClass>]
[<Sealed>]
type ContextMenuService = class end

NameDescription
System_CAPS_pubpropertySystem_CAPS_staticContextMenu

Gets or sets the content of a ContextMenu.

System_CAPS_pubpropertySystem_CAPS_staticHasDropShadow

Gets or sets a value that indicates whether the ContextMenu has the drop shadow effect enabled.

System_CAPS_pubpropertySystem_CAPS_staticHorizontalOffset

Gets or sets a value that indicates where along the x-direction to place the ContextMenu with respect to the parent control.

System_CAPS_pubpropertySystem_CAPS_staticIsEnabled

Gets or sets a value that indicates whether the ContextMenu can be shown.

System_CAPS_pubpropertySystem_CAPS_staticPlacement

Gets or sets a value that specifies the placement of the ContextMenu relative to the PlacementTarget or PlacementRectangle.

System_CAPS_pubpropertySystem_CAPS_staticPlacementRectangle

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

System_CAPS_pubpropertySystem_CAPS_staticPlacementTarget

Gets or sets the parent control of the ContextMenu.

System_CAPS_pubpropertySystem_CAPS_staticShowOnDisabled

Gets or sets a value that indicates whether the ContextMenu should be shown when its parent is grayed out.

System_CAPS_pubpropertySystem_CAPS_staticVerticalOffset

Gets or sets a value that indicates where along the y-direction to place the ContextMenu with respect to the parent control.

NameDescription
System_CAPS_pubmethodSystem_CAPS_staticAddContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler)

Adds a handler for the ContextMenuClosing attached event.

System_CAPS_pubmethodSystem_CAPS_staticAddContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler)

Adds a handler for the ContextMenuOpening attached event.

System_CAPS_pubmethodSystem_CAPS_staticGetContextMenu(DependencyObject)

Gets the value of the ContextMenu property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticGetHasDropShadow(DependencyObject)

Gets a value that indicates whether the ContextMenu has a drop shadow.

System_CAPS_pubmethodSystem_CAPS_staticGetHorizontalOffset(DependencyObject)

Gets the value of the HorizontalOffset property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticGetIsEnabled(DependencyObject)

Gets the value of the IsEnabled property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticGetPlacement(DependencyObject)

Gets the value of the Placement property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticGetPlacementRectangle(DependencyObject)

Gets the value of the PlacementRectangle property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticGetPlacementTarget(DependencyObject)

Gets the value of the PlacementTarget property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticGetShowOnDisabled(DependencyObject)

Gets the value of the ShowOnDisabled property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticGetVerticalOffset(DependencyObject)

Gets the value of the VerticalOffset property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticRemoveContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler)

Removes a handler for the ContextMenuClosing attached event.

System_CAPS_pubmethodSystem_CAPS_staticRemoveContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler)

Removes a handler for the ContextMenuOpening attached event.

System_CAPS_pubmethodSystem_CAPS_staticSetContextMenu(DependencyObject, ContextMenu)

Sets the value of the ContextMenu property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticSetHasDropShadow(DependencyObject, Boolean)

Sets a value that indicates whether the ContextMenu has a drop shadow.

System_CAPS_pubmethodSystem_CAPS_staticSetHorizontalOffset(DependencyObject, Double)

Sets the value of the HorizontalOffset property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticSetIsEnabled(DependencyObject, Boolean)

Sets the value of the IsEnabled property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticSetPlacement(DependencyObject, PlacementMode)

Sets the value of the Placement property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticSetPlacementRectangle(DependencyObject, Rect)

Sets the value of the PlacementRectangle property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticSetPlacementTarget(DependencyObject, UIElement)

Sets the value of the PlacementTarget property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticSetShowOnDisabled(DependencyObject, Boolean)

Sets the value of the ShowOnDisabled property of the specified object.

System_CAPS_pubmethodSystem_CAPS_staticSetVerticalOffset(DependencyObject, Double)

Sets the value of the VerticalOffset property of the specified object.

NameDescription
System_CAPS_pubfieldSystem_CAPS_staticContextMenuClosingEvent

Identifies the ContextMenuClosing routed event.

System_CAPS_pubfieldSystem_CAPS_staticContextMenuOpeningEvent

Identifies the ContextMenuOpening attached event.

System_CAPS_pubfieldSystem_CAPS_staticContextMenuProperty

Identifies the ContextMenu attached property.

System_CAPS_pubfieldSystem_CAPS_staticHasDropShadowProperty

Identifies the HasDropShadow attached property.

System_CAPS_pubfieldSystem_CAPS_staticHorizontalOffsetProperty

Identifies the HorizontalOffset attached property.

System_CAPS_pubfieldSystem_CAPS_staticIsEnabledProperty

Identifies the IsEnabled attached property.

System_CAPS_pubfieldSystem_CAPS_staticPlacementProperty

Identifies the Placement attached property.

System_CAPS_pubfieldSystem_CAPS_staticPlacementRectangleProperty

Identifies the PlacementRectangle attached property.

System_CAPS_pubfieldSystem_CAPS_staticPlacementTargetProperty

Identifies the PlacementTarget attached property.

System_CAPS_pubfieldSystem_CAPS_staticShowOnDisabledProperty

Identifies the ShowOnDisabled attached property.

System_CAPS_pubfieldSystem_CAPS_staticVerticalOffsetProperty

Identifies the VerticalOffset attached property.

NameDescription
System_CAPS_pubeventSystem_CAPS_staticContextMenuClosing

Occurs when the ContextMenu closes.

System_CAPS_pubeventSystem_CAPS_staticContextMenuOpening

Occurs when the ContextMenu opens.

The ContextMenuService class provides attached properties that can be used to specify the appearance and position of a context menu. Many of the properties in the ContextMenuService class are also defined by the ContextMenu. Sometimes it is more convenient to set the properties by using the ContextMenuService class than by setting the properties on a ContextMenu. For example, you can create a ContextMenu to be shared by multiple elements, but use the ContextMenuService class to specify a different position of the ContextMenu for each element. 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.

You can also use the ContextMenuService to display menus on disabled items.

The following example assigns the same ContextMenu to two buttons and sets the HasDropShadow, Placement, PlacementRectangle, HorizontalOffset, and VerticalOffset properties to set the ContextMenu to different positions for each button.

<StackPanel>
  <StackPanel.Resources>
    <ContextMenu x:Key="myContextMenu">
      <MenuItem Header="Item"/>
    </ContextMenu>
  </StackPanel.Resources>

  <!--Both buttons use the same ContextMenu but use the
    properties on ContextMenuService to position them
    differently.-->
  <Button ContextMenu="{StaticResource myContextMenu}" 
          ContextMenuService.HasDropShadow="False" 
          ContextMenuService.Placement="Relative"
          ContextMenuService.HorizontalOffset="50"
          ContextMenuService.VerticalOffset="-10">
    button 1
  </Button>

  <Button ContextMenu="{StaticResource myContextMenu}" 
          ContextMenuService.HasDropShadow="True"
          ContextMenuService.Placement="Right"
          ContextMenuService.PlacementRectangle="0,0,30,30">
    button 2
  </Button>
</StackPanel>

The following example shows how to use the service to display a ContextMenu on a disabled button. Notice that you set the ShowOnDisabled property on the button that is the parent of the context menu.

<Button Height="30" Content="Disabled Button" IsEnabled="False" 
     ContextMenuService.ShowOnDisabled="True">
  <Button.ContextMenu>
    <ContextMenu>
      <MenuItem Header="Item 1"/>
      <MenuItem Header="Item 2"/>
      <MenuItem Header="Item 3"/>
    </ContextMenu>
  </Button.ContextMenu>
</Button>

.NET Framework
Available since 3.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: