WPF: How to open a ContextMenu from all areas of a Grid control
It has been a several months since I've done any solid WPF development (was moved from application development to an actual game team).
From memory, you want to set the ContextMenu
property on the Border
and then set the Border.Background=Transparent
. Setting the background to transparent ensures that it will be participate in the hit detection.
Alternative solution, would be ensure you Grid
element stretches horizontally and vertically to fit the container.
...
Also pull the ContextMenu
out as a static resource, so that is will be easier to find/edit in the future.
Hope this helps (and my memory does not fail me).
EDIT: I have answered a similar question on StackOverflow previously, please see my answer on WPF: Displaying a Context Menu for a GridView's Items. This answer is more complete as it sets the focus on the ListItem
.
As you have already realized, - thanks to Dennis, - you have to set some background. Yes, transparent background is also OK:
<Grid Background="Transparent"> <Grid.ContextMenu> <ContextMenu> <MenuItem Header = "Menu item 1"/> <MenuItem Header = "Menu item 2"/> <MenuItem Header = "Menu item 3"/> </ContextMenu> </Grid.ContextMenu> ........ ........ </Grid>
REFERENCE: https://blogs.msdn.microsoft.com/domgreen/2008/12/08/wpf-hit-testing-with-event-bubbling/