I have a context menu to show up manually by pressing the hotkey ctrl+menu. Therefore i use this function:
ContextMenu.IsOpen = true;
I call this in my main window. But it has some odd effects.
my menu is this one:
<Window.ContextMenu>
<ContextMenu Placement="Center">
<MenuItem IsCheckable="False" Name="item2" Click="MenuItem_Click" Header="{DynamicResource countDownNotificationOn}"/>
</ContextMenu>
</Window.ContextMenu>
using the xaml placement above dosen't work either. Therefore I set the window to
ContextMenuService.Placement="Center"
But doesn't work.
A context menu is a pop-up menu that provides shortcuts for actions the software developer anticipates the user might want to take. In a Windows environment, the context menu is accessed with a right mouse click.
If you want to change the position of the ContextMenu, set the ContextMenuService. Placement property on the FrameworkElement or FrameworkContentElement. You can position a ContextMenu by setting the PlacementTarget, PlacementRectangle, Placement, HorizontalOffset, and VerticalOffsetProperty properties.
The context menu (right-hand mouse button or SHIFT+F10 ) allows you to display a context-sensitive menu. The context is defined by the position of the mouse pointer when the user requests the menu. A context menu allows the user to choose functions that are relevant to the current context.
Press Shift+F10 or Ctrl+Shift+F10 keys on the keyboard to launch the context menu anywhere on your Windows 10 computer.
You need to set the PlacementTarget
property of the ContextMenu
:
if (element.ContextMenu != null )
{
element.ContextMenu.PlacementTarget = element;
element.ContextMenu.IsOpen = true;
}
If after this, the ContextMenu
is still not placed correctly, you can set the placement using the ContextMenu.HorizontalOffset
and ContextMenu.VerticalOffset
properties. Take a look at the ContextMenu.HorizontalOffset Property and ContextMenu.VerticalOffset Property pages at MSDN for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With