【问题标题】:How can I tell a ContextMenu to place itself relatively to its control and not the cursor? [duplicate]如何告诉 ContextMenu 相对于它的控件而不是光标放置自己? [复制]
【发布时间】:2011-04-25 01:30:46
【问题描述】:

我有一个带有默认命令的按钮和一个用于其他可用命令的上下文菜单:

<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>

默认情况下,上下文菜单从光标的热点开始出现:

但是,我希望它出现在按钮下方的固定相对位置(假的,经过编辑的屏幕截图):

设置上下文菜单的PlacementPlacementRectanglePlacementTarget 属性似乎没有任何作用;无论我右键单击我的按钮,上下文菜单都坚持悬停光标。更糟糕的是,聚焦按钮并点击menu key 会导致上下文菜单位于按钮前面,从而完全阻止它。

那么,我究竟如何指定上下文菜单应出现在按钮下方?

【问题讨论】:

    标签: .net wpf xaml contextmenu


    【解决方案1】:

    查看ContextMenu.Placement下的备注

    试试这个

    <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>
    

    【讨论】:

    • 谢谢。你知道为什么在ContextMenu 上设置Placement 不起作用吗?
    • @BoltClock 我不确定。但请查看 ContextMenu.Placement 的备注部分,其中声明设置 ContextMenuService.Placement
    • 备注部分恰好有答案:“当ContextMenu 分配给FrameworkElement.ContextMenuFrameworkContentElement.ContextMenu 属性时,ContextMenuService 在@987654332 时更改此属性的此值@ 打开。”现在一切就绪。
    猜你喜欢
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多