我想通过绑定到ICommand
衍生对象列表来避免必须在 XAML 或代码中手动构建菜单。但是,我遇到了一些问题,其中生成的菜单有两个级别的菜单项(即每个MenuItem
都包含在 a 中MenuItem
):
我的猜测是,这是因为 WPF 会自动MenuItem
为我的绑定生成 a,但实际上我使用的“查看器”已经是a MenuItem
(它派生自MenuItem
):
<ContextMenu
x:Name="selectionContextMenu"
ItemsSource="{Binding Source={x:Static OrangeNote:Note.MultiCommands}}"
ItemContainerStyleSelector="{StaticResource separatorStyleSelector}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<Viewers:NoteCommandMenuItemViewer
CommandParameter="{Binding Source={x:Static OrangeNote:App.Screen}, Path=SelectedNotes}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
(ItemContainerStyleSelector 来自http://bea.stollnitz.com/blog/?p=23,它允许我Separator
在绑定源中包含元素。)
因此,菜单绑定到ICommand
s 的集合,并且每个项目CommandParameter
都设置为相同的全局目标(恰好是一个集合,但这并不重要)。
我的问题是,有什么方法可以绑定它,以使 WPF 不会自动将每个项目包装在MenuItem
?