WPF ContextMenu 使用 ItemsControl,错误地突出显示整个集合

标签 wpf contextmenu highlight itemscontrol

我需要设计一个ContextMenu,其中包含一个MenuItem,这个MenuItem有一个更深层次的菜单列表,它绑定(bind)到一个类型的属性ObservableCollection 来 self 的 ViewModel。代码如下所示:

<ContextMenu DataContext="{Binding PlacementTarget.DataContext, 
   RelativeSource={RelativeSource Self}}" >            
   ...                                                                                                                                                     
   <MenuItem Header="Map to account" >
       <ItemsControl ItemsSource="{Binding RelatedAccounts}" >                                        
           <ItemsControl.ItemTemplate>
               <DataTemplate>
                   <MenuItem Header="{Binding Number}" 
                        Command="{Binding PlacementTarget.DataContext.MapToAccountCommand, 
                        RelativeSource={RelativeSource AncestorType=ContextMenu}}"
                        CommandParameter="{Binding}"
                   />                 
               </DataTemplate>
           </ItemsControl.ItemTemplate>                                        
       </ItemsControl>
   </MenuItem>
...
</ContextMenu>

想法是,当用户从 UI 中右键单击一个付款项,然后转到“映射到帐户”菜单项时,将显示更深层次的菜单项并列出所有相关帐户供用户选择(如您所见,ItemsControl 绑定(bind)到 RelatedAccounts)

一切正常,上下文菜单正确显示我从 ViewModel 公开的所有相关帐户,当用户右键单击一个帐户时,Command 属性 MapToAccountCommand 来自ViewModel 使用所选帐户的传递参数执行。

但有一种行为是我不想要的:当鼠标进入比“映射到帐户”菜单更深一层时,它实际上突出显示了整个菜单项集合。请看下面的图片:

enter image description here

以上是我把鼠标放在“美元账户1”上的情况

即使鼠标不在任何特定帐户上,而是在更深层次菜单中的其他区域,高亮效果仍然存在,请看图片:

enter image description here

这显然感觉不对。谁能告诉我我做错了什么?谢谢!

最佳答案

MemnuItem 已经是 ItemsControl,因此它有自己的 ItemsSource 属性,您可以绑定(bind)到该属性。尝试这样的事情:

<MenuItem Header="Map to account" ItemsSource="{Binding RelatedAccounts}" >                                        
     <MenuItem.ItemTemplate>
         <DataTemplate>
             <TextBlock Text="{Binding Number}"/>
         </DataTemplate>
     </MenuItem.ItemTemplate>                                        
     <MenuItem.ItemContainerStyle>
         <Style TargetType="{x:Type MenuItem}">
             <Setter Property="Command" Value="{Binding PlacementTarget.DataContext.MapToAccountCommand, 
                  RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
             <Setter Property="CommandParameter" Value="{Binding}"/>
         </Style>
     </MenuItem.ItemContainerStyle>
</MenuItem>

在您的情况下,您将 ItemsControl 作为 MenuItem 项,因此 WPF 会将其包装在 MenuItem 和您的整个 ItemsControl 与列表中的其他 MenuItem 成为一个大的 MenuItem

关于WPF ContextMenu 使用 ItemsControl,错误地突出显示整个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17449177/

相关文章:

c# - 列表框中的扩展器在折叠时留下空白

wpf - WPF文本框字符串格式的奇怪问题 - 光标向后移动

c# - 在 WPF 日历上标记特定日期

ios - 当弹出窗口消失时,保持 UITextView 中的文本突出显示

html - Ionic 2 输入突出显示

c# - 'method' 匹配委托(delegate) 'RoutedEventHandler' 没有重载

javascript - 在加载页面时从指令编译 HTML 字符串

javascript - 使用 javascript 来防止 CTRL+Leftclick 上的上下文菜单

windows - 查找并模拟点击系统托盘图标?

javascript - 获取突出显示的文本