ContextMenuService 类

定义

提供用于显示 ContextMenu 的系统实现。Provides the system implementation for displaying a ContextMenu.

public ref class ContextMenuService abstract sealed
public static class ContextMenuService
type ContextMenuService = class
Public Class ContextMenuService
继承
ContextMenuService

示例

下面的ContextMenu示例将相同的赋给两个按钮, 并Placement HasDropShadow设置PlacementRectangleHorizontalOffset、、 VerticalOffset和属性, 以ContextMenu将每个按钮的位置设置为不同的位置。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>

下面的示例演示如何使用服务ContextMenu在禁用的按钮上显示。The following example shows how to use the service to display a ContextMenu on a disabled button. 请注意, 在作为ShowOnDisabled上下文菜单父级的按钮上设置属性。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>

注解

ContextMenuService类提供附加属性, 这些属性可用于指定上下文菜单的外观和位置。The ContextMenuService class provides attached properties that can be used to specify the appearance and position of a context menu. ContextMenuService类中的许多属性还ContextMenu由定义。Many of the properties in the ContextMenuService class are also defined by the ContextMenu. 有时通过使用ContextMenuService类来设置属性比ContextMenu在上设置属性更方便。Sometimes it is more convenient to set the properties by using the ContextMenuService class than by setting the properties on a ContextMenu. 例如, 你可以创建一个ContextMenu由多个元素共享的, 但ContextMenuService使用类为每个元素指定的位置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. 以下属性由ContextMenuContextMenuService类定义。The following properties are defined by the ContextMenu and ContextMenuService classes. 如果在ContextMenu ContextMenuService和上都设置了这些属性中的任何一个,则使用中的属性值。ContextMenuServiceIf any of these properties are set on both ContextMenu and the ContextMenuService, the property value from the ContextMenuService is used.

你还可以使用ContextMenuService来显示禁用项的菜单。You can also use the ContextMenuService to display menus on disabled items.

字段

ContextMenuClosingEvent

标识 ContextMenuClosing 路由事件。Identifies the ContextMenuClosing routed event.

ContextMenuOpeningEvent

标识 ContextMenuOpening 附加事件。Identifies the ContextMenuOpening attached event.

ContextMenuProperty

标识 ContextMenu 附加属性。Identifies the ContextMenu attached property.

HasDropShadowProperty

标识 HasDropShadow 附加属性。Identifies the HasDropShadow attached property.

HorizontalOffsetProperty

标识 HorizontalOffset 附加属性。Identifies the HorizontalOffset attached property.

IsEnabledProperty

标识 IsEnabled 附加属性。Identifies the IsEnabled attached property.

PlacementProperty

标识 Placement 附加属性。Identifies the Placement attached property.

PlacementRectangleProperty

标识 PlacementRectangle 附加属性。Identifies the PlacementRectangle attached property.

PlacementTargetProperty

标识 PlacementTarget 附加属性。Identifies the PlacementTarget attached property.

ShowOnDisabledProperty

标识 ShowOnDisabled 附加属性。Identifies the ShowOnDisabled attached property.

VerticalOffsetProperty

标识 VerticalOffset 附加属性。Identifies the VerticalOffset attached property.

附加属性

ContextMenu

获取或设置 ContextMenu 的内容。Gets or sets the content of a ContextMenu.

HasDropShadow

获取或设置指示 ContextMenu 是否已启用投影效果的值。Gets or sets a value that indicates whether the ContextMenu has the drop shadow effect enabled.

HorizontalOffset

获取或设置指示沿 x 方向相对于父控件放置 ContextMenu 的位置的值。Gets or sets a value that indicates where along the x-direction to place the ContextMenu with respect to the parent control.

IsEnabled

获取或设置指示是否可以显示 ContextMenu 的值。Gets or sets a value that indicates whether the ContextMenu can be shown.

Placement

获取或设置指定 ContextMenu 相对于 PlacementTargetPlacementRectangle 的位置的值。Gets or sets a value that specifies the placement of the ContextMenu relative to the PlacementTarget or PlacementRectangle.

PlacementRectangle

获取或设置上下文菜单打开时与其所在位置相对的区域。Gets or sets the area relative to which the context menu is positioned when it opens.

PlacementTarget

获取或设置 ContextMenu 的父控件。Gets or sets the parent control of the ContextMenu.

ShowOnDisabled

获取或设置指示是否在 ContextMenu 的父级变为灰色时应显示该项的值。Gets or sets a value that indicates whether the ContextMenu should be shown when its parent is grayed out.

VerticalOffset

获取或设置指示沿 y 方向相对于父控件放置 ContextMenu 的位置的值。Gets or sets a value that indicates where along the y-direction to place the ContextMenu with respect to the parent control.

方法

AddContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler)

ContextMenuClosing 附加事件添加处理程序。Adds a handler for the ContextMenuClosing attached event.

AddContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler)

ContextMenuOpening 附加事件添加处理程序。Adds a handler for the ContextMenuOpening attached event.

GetContextMenu(DependencyObject)

获取指定对象的 ContextMenu 属性的值。Gets the value of the ContextMenu property of the specified object.

GetHasDropShadow(DependencyObject)

获取指示 ContextMenu 是否有投影的值。Gets a value that indicates whether the ContextMenu has a drop shadow.

GetHorizontalOffset(DependencyObject)

获取指定对象的 HorizontalOffset 属性的值。Gets the value of the HorizontalOffset property of the specified object.

GetIsEnabled(DependencyObject)

获取指定对象的 IsEnabled 属性的值。Gets the value of the IsEnabled property of the specified object.

GetPlacement(DependencyObject)

获取指定对象的 Placement 属性的值。Gets the value of the Placement property of the specified object.

GetPlacementRectangle(DependencyObject)

获取指定对象的 PlacementRectangle 属性的值。Gets the value of the PlacementRectangle property of the specified object.

GetPlacementTarget(DependencyObject)

获取指定对象的 PlacementTarget 属性的值。Gets the value of the PlacementTarget property of the specified object.

GetShowOnDisabled(DependencyObject)

获取指定对象的 ShowOnDisabled 属性的值。Gets the value of the ShowOnDisabled property of the specified object.

GetVerticalOffset(DependencyObject)

获取指定对象的 VerticalOffset 属性的值。Gets the value of the VerticalOffset property of the specified object.

RemoveContextMenuClosingHandler(DependencyObject, ContextMenuEventHandler)

移除 ContextMenuClosing 附加事件的处理程序。Removes a handler for the ContextMenuClosing attached event.

RemoveContextMenuOpeningHandler(DependencyObject, ContextMenuEventHandler)

移除 ContextMenuOpening 附加事件的处理程序。Removes a handler for the ContextMenuOpening attached event.

SetContextMenu(DependencyObject, ContextMenu)

设置指定对象的 ContextMenu 属性的值。Sets the value of the ContextMenu property of the specified object.

SetHasDropShadow(DependencyObject, Boolean)

设置指示 ContextMenu 是否有投影的值。Sets a value that indicates whether the ContextMenu has a drop shadow.

SetHorizontalOffset(DependencyObject, Double)

设置指定对象的 HorizontalOffset 属性的值。Sets the value of the HorizontalOffset property of the specified object.

SetIsEnabled(DependencyObject, Boolean)

设置指定对象的 IsEnabled 属性的值。Sets the value of the IsEnabled property of the specified object.

SetPlacement(DependencyObject, PlacementMode)

设置指定对象的 Placement 属性的值。Sets the value of the Placement property of the specified object.

SetPlacementRectangle(DependencyObject, Rect)

设置指定对象的 PlacementRectangle 属性的值。Sets the value of the PlacementRectangle property of the specified object.

SetPlacementTarget(DependencyObject, UIElement)

设置指定对象的 PlacementTarget 属性的值。Sets the value of the PlacementTarget property of the specified object.

SetShowOnDisabled(DependencyObject, Boolean)

设置指定对象的 ShowOnDisabled 属性的值。Sets the value of the ShowOnDisabled property of the specified object.

SetVerticalOffset(DependencyObject, Double)

设置指定对象的 VerticalOffset 属性的值。Sets the value of the VerticalOffset property of the specified object.

附加事件

ContextMenuClosing

ContextMenu 关闭时发生。Occurs when the ContextMenu closes.

ContextMenuOpening

ContextMenu 打开时发生。Occurs when the ContextMenu opens.

适用于

另请参阅