c# - WPF MenuItem 未填充可用的 ContextMenu 空间

标签 c# wpf user-interface mahapps.metro

我在使用没有任何额外样式的 MahApps.Metro 的简单 ContextMenu 时遇到了一个奇怪的问题。将光标移动到文本顶部或稍微围绕它移动时,没有问题。但是当它移得更远时,仍然在 ContextMenu 范围内,Cursor 不再位于 MenuItem 的顶部。除了关闭上下文菜单外,现在单击也不会导致任何操作。

enter image description here enter image description here

<ContextMenu ItemsSource="{Binding ContextItems}">
    <ContextMenu.ItemTemplate>
        <DataTemplate>
            <MenuItem Header="{Binding Text}" Command="{Binding Command}"/>
        </DataTemplate>
    </ContextMenu.ItemTemplate>
</ContextMenu>

我做错了什么?为什么 MenuItem 不使用可用空间?

最佳答案

如果您的 ContextItems 包含带有 View 模型的集合,那么我认为这可以帮助您(未测试):

<ContextMenu ItemsSource="{Binding ContextItems}">
    <ContextMenu.ItemContainerStyle>
        <Style TargetType="MenuItem">
            <Setter Property="Header" Value="{Binding Text}" />
            <Setter Property="Command" Value="{Binding Command}" />
        </Style>
    </ContextMenu.ItemContainerStyle>
</ContextMenu>

Command 和 Text 应该是 viewmodel 对象的属性。

关于c# - WPF MenuItem 未填充可用的 ContextMenu 空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35791455/

相关文章:

WPF:如果将按钮放在工具栏中,按钮助记符不起作用

python - 我如何使用 tkinter python 与 phrase = input ("ENTER TEXT ") Tkinter 文本框?

delphi - (如何)我可以使用带有标准文件打开对话框的 FutureWindows 吗?

除英语之外的 C# WritePrivateProfileString() 值

wpf - 使用 WPFToolKit 的 DataGridHyperlinkColumn

c# - 删除windows不想让我删除的文件?

c# - 集合更新后转换器未触发

user-interface - 命令行SSH远程服务器管理器的UI客户端

c# - 使用 XslCompiledTransform 的正确方法是什么?

c# - double.MaxValue 和 double.MinValue 在以下上下文中意味着什么?