Export (0) Print
Expand All

ContextMenuService Class

Provides the system implementation for displaying a ContextMenu.

System.Object
  System.Windows.Controls.ContextMenuService

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

public static class ContextMenuService

The ContextMenuService type exposes the following members.

  NameDescription
Public attached propertyContextMenuGets or sets the content of a ContextMenu.
Public attached propertyHasDropShadowGets or sets a value that indicates whether the ContextMenu has the drop shadow effect enabled.
Public attached propertyHorizontalOffsetGets or sets a value that indicates where along the x-direction to place the ContextMenu with respect to the parent control.
Public attached propertyIsEnabledGets or sets a value that indicates whether the ContextMenu can be shown.
Public attached propertyPlacementGets or sets a value that specifies the placement of the ContextMenu relative to the PlacementTarget or PlacementRectangle.
Public attached propertyPlacementRectangleGets or sets the area relative to which the context menu is positioned when it opens.
Public attached propertyPlacementTargetGets or sets the parent control of the ContextMenu.
Public attached propertyShowOnDisabledGets or sets a value that indicates whether the ContextMenu should be shown when its parent is grayed out.
Public attached propertyVerticalOffsetGets or sets a value that indicates where along the y-direction to place the ContextMenu with respect to the parent control.
Top

  NameDescription
Public methodStatic memberAddContextMenuClosingHandlerAdds a handler for the ContextMenuClosing attached event.
Public methodStatic memberAddContextMenuOpeningHandlerAdds a handler for the ContextMenuOpening attached event.
Public methodStatic memberGetContextMenu Gets the value of the ContextMenu property of the specified object.
Public methodStatic memberGetHasDropShadowGets a value that indicates whether the ContextMenu has a drop shadow.
Public methodStatic memberGetHorizontalOffset Gets the value of the HorizontalOffset property of the specified object.
Public methodStatic memberGetIsEnabled Gets the value of the IsEnabled property of the specified object.
Public methodStatic memberGetPlacement Gets the value of the Placement property of the specified object.
Public methodStatic memberGetPlacementRectangle Gets the value of the PlacementRectangle property of the specified object.
Public methodStatic memberGetPlacementTarget Gets the value of the PlacementTarget property of the specified object.
Public methodStatic memberGetShowOnDisabled Gets the value of the ShowOnDisabled property of the specified object.
Public methodStatic memberGetVerticalOffset Gets the value of the VerticalOffset property of the specified object.
Public methodStatic memberRemoveContextMenuClosingHandlerRemoves a handler for the ContextMenuClosing attached event.
Public methodStatic memberRemoveContextMenuOpeningHandlerRemoves a handler for the ContextMenuOpening attached event.
Public methodStatic memberSetContextMenuSets the value of the ContextMenu property of the specified object.
Public methodStatic memberSetHasDropShadowSets a value that indicates whether the ContextMenu has a drop shadow.
Public methodStatic memberSetHorizontalOffset Sets the value of the HorizontalOffset property of the specified object.
Public methodStatic memberSetIsEnabled Sets the value of the IsEnabled property of the specified object.
Public methodStatic memberSetPlacement Sets the value of the Placement property of the specified object.
Public methodStatic memberSetPlacementRectangle Sets the value of the PlacementRectangle property of the specified object.
Public methodStatic memberSetPlacementTarget Sets the value of the PlacementTarget property of the specified object.
Public methodStatic memberSetShowOnDisabled Sets the value of the ShowOnDisabled property of the specified object.
Public methodStatic memberSetVerticalOffset Sets the value of the VerticalOffset property of the specified object.
Top

  NameDescription
Public attached eventContextMenuClosingOccurs when the ContextMenu closes.
Public attached eventContextMenuOpeningOccurs when the ContextMenu opens.
Top

  NameDescription
Public fieldStatic memberContextMenuClosingEvent Identifies the ContextMenuClosing routed event.
Public fieldStatic memberContextMenuOpeningEventIdentifies the ContextMenuOpening attached event.
Public fieldStatic memberContextMenuProperty Identifies the ContextMenu attached property.
Public fieldStatic memberHasDropShadowPropertyIdentifies the HasDropShadow attached property.
Public fieldStatic memberHorizontalOffsetProperty Identifies the HorizontalOffset attached property.
Public fieldStatic memberIsEnabledProperty Identifies the IsEnabled attached property.
Public fieldStatic memberPlacementProperty Identifies the Placement attached property.
Public fieldStatic memberPlacementRectangleProperty Identifies the PlacementRectangle attached property.
Public fieldStatic memberPlacementTargetProperty Identifies the PlacementTarget attached property.
Public fieldStatic memberShowOnDisabledProperty Identifies the ShowOnDisabled attached property.
Public fieldStatic memberVerticalOffsetProperty Identifies the VerticalOffset attached property.
Top

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

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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