使用ItemsControl的WPF ContextMenu错误地突出显示了整个集合 [英] WPF ContextMenu using ItemsControl, incorrectly highlight the whole collection

查看:73
本文介绍了使用ItemsControl的WPF ContextMenu错误地突出显示了整个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设计一个ContextMenu,其中包括一个MenuItem,该MenuItem具有更深层次的菜单列表,该菜单列表绑定到ViewModel中类型为ObservableCollection的属性.代码如下:

I need to design one ContextMenu which includes one MenuItem, this MenuItem has a deeper level menu list which binds to a property of type ObservableCollection from my ViewModel. The code looks like this:

<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绑定到RelatedAccounts)

The idea is when the user right-clicks one payment item from the UI, and goes to "Map to account" menu item, a deeper level of menu items will show up and list all the related account for the user to select(as you can see the ItemsControl binds to RelatedAccounts)

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

Everything works fine, the context menu correctly shows all the related accounts I expose from ViewModel, and when the user right clicks on one account, the Command property MapToAccountCommand from the ViewModel gets executed with the passed parameter of selected account.

但是我不希望有一种行为:当鼠标比菜单映射到帐户"更深一层时,它实际上会突出显示菜单项的整个集合.请看下面的图片:

But there is one behavior that I don't want: when the mouse goes into one level deeper than the menu "Map to account", it actually highlights the whole collection of menu items. Please see the pictures below:

在上述情况下,我将鼠标放在"USD Account 1"上

Above is the case when I put my mouse over "USD Account 1"

即使鼠标不在任何特定帐户上,而是在更深层次菜单中的某些其他区域上,突出显示效果仍然存在,请参见图片:

And even if the mouse is not over any specific account, but in some other area within the deeper level menu, the highlight effect is still there, see the picture:

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

This obviously doesn't feel about right. Can anyone tell me what I did wrong? Thanks!

推荐答案

MemnuItem已经是ItemsControl,因此具有您可以绑定到的自己的ItemsSource属性.尝试这样的事情:

MemnuItem is already ItemsControl therefore has its own ItemsSource property which you can bind to. Try something like this:

<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,而列表中的其他MenuItems

In your case you put ItemsControl as MenuItem item therefore WPF will wrap it in MenuItem and your whole ItemsControl becomes one big MenuItem with other MenuItems in the list

这篇关于使用ItemsControl的WPF ContextMenu错误地突出显示了整个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆