ContextMenu.PlacementTarget Property

Definition

Gets or sets the UIElement relative to which the ContextMenu is positioned when it opens.

C#
[System.ComponentModel.Bindable(true)]
public System.Windows.UIElement PlacementTarget { get; set; }

Property Value

The element relative to which the ContextMenu is positioned when it opens. The default is null.

Attributes

Examples

The target of a ContextMenu is its parent. The following examples show how to use the PlacementTarget property to find the parent of a ContextMenu.

C#
ContextMenu cm = (ContextMenu)sender;
target = cm.PlacementTarget;
C#
ContextMenu cm = (ContextMenu)ContextMenu.ItemsControlFromItemContainer                   ((MenuItem)e.OriginalSource);
UIElement placementTarget = cm.PlacementTarget;

Remarks

When the ContextMenu is assigned to the FrameworkElement.ContextMenu or FrameworkContentElement.ContextMenu property, the ContextMenuService changes this value of this property to the owning FrameworkElement or FrameworkContentElement when the ContextMenu opens. To use a different UIElement, set the ContextMenuService.PlacementTarget property.

Dependency Property Information

Item Value
Identifier field PlacementTargetProperty
Metadata properties set to true None

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also