我有一个如下所示的ContextMenu:
<ContextMenu StaysOpen="False" Width="150" x:Name="HistoryContextMenu">
<MenuItem Command="{Binding HistorySetCurrent}" Header="Set _Current (C)"/>
<MenuItem Command="{Binding HistorySetReference}" Header="Set _Reference (R)" />
<MenuItem Header="Add to Group" ItemsSource="{Binding ElementName=Groups, Path=ChartGroups}" DisplayMemberPath="Name" HorizontalAlignment="Left">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseUp">
<cmd:EventToCommand Command="{Binding HistoryGroupContextClickCommand}" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=MenuItem}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</MenuItem>
</ContextMenu>
我正在使用MVVM,所以我绑定到一个方法来处理PreviewMouseUp事件(我也尝试过其他事件)。我在子菜单中得到了一个列表,但是当我点击其中一个列表时,我得到的eventargs包含列表中的所有菜单项,而不仅仅是我选择的菜单项,没有选择的那个。 如何找到所选的menuitem?我已经尝试使用代码来执行此操作,但该方法也会出现同样的问题。
答案 0 :(得分:1)
您应该使用ItemContainerStyle
来管理生成的Commands
上的MenuItems
。
看看Kent对此问题的回答,该问题向您展示了如何设置样式:Using a DataTemplate for a MenuItem causes extra space on the left side to appear?
您需要将Command
和单个项目的绑定添加为CommandParameter
,以便为Command
提供上下文。