Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell a ContextMenu to place itself relatively to its control and not the cursor? [duplicate]

I have a button with a default command and a context menu for other available commands:

<Button Content="Do this" Height="23" Width="75" Command="local:MyCommands.ThisCommand">     <Button.ContextMenu>         <ContextMenu>             <MenuItem Header="Do this" Command="local:MyCommands.ThisCommand" />             <MenuItem Header="Do that" Command="local:MyCommands.ThatCommand" />         </ContextMenu>     </Button.ContextMenu> </Button> 

By default, the context menu appears starting at the hot spot of the cursor:

However, I'd like it to appear at a fixed relative position, beneath the button (fake, edited screenshot):

Setting the context menu's Placement, PlacementRectangle and PlacementTarget properties doesn't seem to do anything; the context menu insists on hanging off the cursor wherever I right-click my button. Worse, focusing the button and hitting the menu key causes the context menu to sit in front of the button, blocking it completely.

So, how exactly do I specify that the context menu should appear beneath the button?

like image 563
BoltClock Avatar asked Apr 25 '11 01:04

BoltClock


People also ask

When you right-click on any icon a pop up menu appears which is called?

A context menu (also know as a contextual menu, shortcut menu or pop-up menu) is the menu that appears when you right-click and offers a set of choices that are available for, or in context of, whatever it was you clicked.

How do you handle context menu?

The contextmenu event fires when the user attempts to open a context menu. This event is typically triggered by clicking the right mouse button, or by pressing the context menu key.

What is the context menu?

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.

What is Contextmenu in Javascript?

A context menu is a menu in a GUI that appears upon user interaction, such as a right-click mouse operation. A context menu offers a limited set of choices that are available in the current state, or context, of the operating system or application.


1 Answers

Check out Remarks under ContextMenu.Placement

and try this

<Button Content="Do this" Height="23" Width="75"       ContextMenuService.Placement="Bottom"      Command="local:MyCommands.ThisCommand">     <Button.ContextMenu>         <ContextMenu>             <MenuItem Header="Do this" Command="local:MyCommands.ThisCommand" />             <MenuItem Header="Do that" Command="local:MyCommands.ThatCommand" />         </ContextMenu>     </Button.ContextMenu> </Button> 
like image 62
Bala R Avatar answered Sep 20 '22 17:09

Bala R