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

被刻印的时光 ゝ 提交于 2019-12-20 10:24:38

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/5774442/how-can-i-tell-a-contextmenu-to-place-itself-relatively-to-its-control-and-not-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!