ContextMenuService ContextMenuService ContextMenuService ContextMenuService Class

Definition

Provides the system implementation for displaying a ContextMenu.

public ref class ContextMenuService abstract sealed
public static class ContextMenuService
type ContextMenuService = class
Public Class ContextMenuService
Inheritance
ContextMenuServiceContextMenuServiceContextMenuServiceContextMenuService

Examples

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>

Remarks

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.

Fields

ContextMenuClosingEvent ContextMenuClosingEvent ContextMenuClosingEvent ContextMenuClosingEvent

Identifies the ContextMenuClosing routed event.

ContextMenuOpeningEvent ContextMenuOpeningEvent ContextMenuOpeningEvent ContextMenuOpeningEvent

Identifies the ContextMenuOpening attached event.

ContextMenuProperty ContextMenuProperty ContextMenuProperty ContextMenuProperty

Identifies the ContextMenu attached property.

HasDropShadowProperty HasDropShadowProperty HasDropShadowProperty HasDropShadowProperty

Identifies the HasDropShadow attached property.

HorizontalOffsetProperty HorizontalOffsetProperty HorizontalOffsetProperty HorizontalOffsetProperty

Identifies the HorizontalOffset attached property.

IsEnabledProperty IsEnabledProperty IsEnabledProperty IsEnabledProperty

Identifies the IsEnabled attached property.

PlacementProperty PlacementProperty PlacementProperty PlacementProperty

Identifies the Placement attached property.

PlacementRectangleProperty PlacementRectangleProperty PlacementRectangleProperty PlacementRectangleProperty

Identifies the PlacementRectangle attached property.

PlacementTargetProperty PlacementTargetProperty PlacementTargetProperty PlacementTargetProperty

Identifies the PlacementTarget attached property.

ShowOnDisabledProperty ShowOnDisabledProperty ShowOnDisabledProperty ShowOnDisabledProperty

Identifies the ShowOnDisabled attached property.

VerticalOffsetProperty VerticalOffsetProperty VerticalOffsetProperty VerticalOffsetProperty

Identifies the VerticalOffset attached property.

Attached Properties

ContextMenu ContextMenu ContextMenu ContextMenu

Gets or sets the content of a ContextMenu.

HasDropShadow HasDropShadow HasDropShadow HasDropShadow

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

HorizontalOffset HorizontalOffset HorizontalOffset HorizontalOffset

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

IsEnabled IsEnabled IsEnabled IsEnabled

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

Placement Placement Placement Placement

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

PlacementRectangle PlacementRectangle PlacementRectangle PlacementRectangle

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

PlacementTarget PlacementTarget PlacementTarget PlacementTarget

Gets or sets the parent control of the ContextMenu.

ShowOnDisabled ShowOnDisabled ShowOnDisabled ShowOnDisabled

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

VerticalOffset VerticalOffset VerticalOffset VerticalOffset

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

Methods

AddContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler) AddContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler) AddContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler) AddContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler)

Adds a handler for the ContextMenuClosing attached event.

AddContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler) AddContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler) AddContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler) AddContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler)

Adds a handler for the ContextMenuOpening attached event.

GetContextMenu(DependencyObject) GetContextMenu(DependencyObject) GetContextMenu(DependencyObject) GetContextMenu(DependencyObject)

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

GetHasDropShadow(DependencyObject) GetHasDropShadow(DependencyObject) GetHasDropShadow(DependencyObject) GetHasDropShadow(DependencyObject)

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

GetHorizontalOffset(DependencyObject) GetHorizontalOffset(DependencyObject) GetHorizontalOffset(DependencyObject) GetHorizontalOffset(DependencyObject)

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

GetIsEnabled(DependencyObject) GetIsEnabled(DependencyObject) GetIsEnabled(DependencyObject) GetIsEnabled(DependencyObject)

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

GetPlacement(DependencyObject) GetPlacement(DependencyObject) GetPlacement(DependencyObject) GetPlacement(DependencyObject)

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

GetPlacementRectangle(DependencyObject) GetPlacementRectangle(DependencyObject) GetPlacementRectangle(DependencyObject) GetPlacementRectangle(DependencyObject)

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

GetPlacementTarget(DependencyObject) GetPlacementTarget(DependencyObject) GetPlacementTarget(DependencyObject) GetPlacementTarget(DependencyObject)

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

GetShowOnDisabled(DependencyObject) GetShowOnDisabled(DependencyObject) GetShowOnDisabled(DependencyObject) GetShowOnDisabled(DependencyObject)

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

GetVerticalOffset(DependencyObject) GetVerticalOffset(DependencyObject) GetVerticalOffset(DependencyObject) GetVerticalOffset(DependencyObject)

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

RemoveContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler) RemoveContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler) RemoveContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler) RemoveContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler)

Removes a handler for the ContextMenuClosing attached event.

RemoveContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler) RemoveContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler) RemoveContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler) RemoveContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler)

Removes a handler for the ContextMenuOpening attached event.

SetContextMenu(DependencyObject, ContextMenu) SetContextMenu(DependencyObject, ContextMenu) SetContextMenu(DependencyObject, ContextMenu) SetContextMenu(DependencyObject, ContextMenu)

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

SetHasDropShadow(DependencyObject, Boolean) SetHasDropShadow(DependencyObject, Boolean) SetHasDropShadow(DependencyObject, Boolean) SetHasDropShadow(DependencyObject, Boolean)

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

SetHorizontalOffset(DependencyObject, Double) SetHorizontalOffset(DependencyObject, Double) SetHorizontalOffset(DependencyObject, Double) SetHorizontalOffset(DependencyObject, Double)

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

SetIsEnabled(DependencyObject, Boolean) SetIsEnabled(DependencyObject, Boolean) SetIsEnabled(DependencyObject, Boolean) SetIsEnabled(DependencyObject, Boolean)

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

SetPlacement(DependencyObject, PlacementMode) SetPlacement(DependencyObject, PlacementMode) SetPlacement(DependencyObject, PlacementMode) SetPlacement(DependencyObject, PlacementMode)

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

SetPlacementRectangle(DependencyObject, Rect) SetPlacementRectangle(DependencyObject, Rect) SetPlacementRectangle(DependencyObject, Rect) SetPlacementRectangle(DependencyObject, Rect)

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

SetPlacementTarget(DependencyObject, UIElement) SetPlacementTarget(DependencyObject, UIElement) SetPlacementTarget(DependencyObject, UIElement) SetPlacementTarget(DependencyObject, UIElement)

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

SetShowOnDisabled(DependencyObject, Boolean) SetShowOnDisabled(DependencyObject, Boolean) SetShowOnDisabled(DependencyObject, Boolean) SetShowOnDisabled(DependencyObject, Boolean)

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

SetVerticalOffset(DependencyObject, Double) SetVerticalOffset(DependencyObject, Double) SetVerticalOffset(DependencyObject, Double) SetVerticalOffset(DependencyObject, Double)

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

Attached Events

ContextMenuClosing ContextMenuClosing ContextMenuClosing ContextMenuClosing

Occurs when the ContextMenu closes.

ContextMenuOpening ContextMenuOpening ContextMenuOpening ContextMenuOpening

Occurs when the ContextMenu opens.

Applies to

See Also