Limit / Move context menu (right click) within the window

Whenever you right-click on the edge of a browser (or any other application), the context menu overflows the window, but not the screen.

I want the context menu to always lie within the window, so when I click on the bottom edge, it should show the menu at the top. Suppose that window sizes will always be larger than context menus.

Can this be done? if not, is there a workaround?

PS I do not create an application window, just a viewer for applications.

Context-menu going out of bounds

+7
c # windows winapi wpf contextmenu
source share
1 answer

Use the Placement and PlacementRectangle properties of your ContextMenu.

In this example below, I set the Placement property to "Relative" and the Rectangle to x, y coordinates 25, 25. Then, no matter where I click, the context menu will appear at 25, 25, relative to the button.

 <Button ContextMenuService.Placement="Relative" ContextMenuService.PlacementRectangle="25, 25, 0, 0"> <Button.ContextMenu> <ContextMenu> <MenuItem Header="Do this" /> <MenuItem Header="Do that" /> </ContextMenu> </Button.ContextMenu> </Button> 

Of course, you could do a more dynamic calculation of the values ​​that will be used for these properties, depending on the size of the menu and its proximity to the edge.

-one
source share

All Articles