.net - 我如何告诉 ContextMenu 将其自身相对于其控件而不是光标放置?

标签 .net wpf xaml contextmenu

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

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

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

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

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

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

最佳答案

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

关于.net - 我如何告诉 ContextMenu 将其自身相对于其控件而不是光标放置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5774442/

相关文章:

sql-server - 设置为Null但不设置为0-int

wpf - WPF:具有ProgressBar的文本框

c# - 找到 TreeViewItem 来放置数据

xaml - 忽略绑定(bind)初始化

c# - 用另一个哈希表更新哈希表?

c# - INotifyPropertyChanged - 为什么要进行额外的分配?

c# - Windows 上的应用程序服务器等效项

c# - 如何检查类型是否永远不是有效的泛型参数?

c# - 为什么需要安装向导?我如何创建一个?

c# - WPF 文档查看器 : Navigate using internal link not accurate on first click