Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ContextMenuService Class

Provides the system implementation for displaying a ContextMenu.

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

public static class ContextMenuService
You cannot directly create an instance of this class in XAML.

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.

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

  <!--Both buttons use the same ContextMenu but use the
    properties on ContextMenuService to position them
  <Button ContextMenu="{StaticResource myContextMenu}" 
    button 1

  <Button ContextMenu="{StaticResource myContextMenu}" 
    button 2

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" 
      <MenuItem Header="Item 1"/>
      <MenuItem Header="Item 2"/>
      <MenuItem Header="Item 3"/>


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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5, 3.0
© 2018 Microsoft