WPF ContextMenu using ItemsControl, incorrectly highlight the whole collection

梦想的初衷 提交于 2019-12-08 20:53:39

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>

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!