marcel marcel - 1 year ago 114
C# Question

Context Menu location

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.

  1. If I only press the menu-key, the menu alwasy appears in the middle of the screen

  2. If I manually call the menu, it always appears in the top left corner.

my menu is this one:

<ContextMenu Placement="Center">
<MenuItem IsCheckable="False" Name="item2" Click="MenuItem_Click" Header="{DynamicResource countDownNotificationOn}"/>

using the xaml placement above dosen't work either. Therefore I set the window to


But doesn't work.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download