我有这个上下文菜单,我在 TreeView 的不同 DataTemplates 中使用它。
<Window.Resources>
<ContextMenu x:Key="mnuContextTreeView">
<ContextMenu.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{StaticResource mnuRun}" />
<Separator />
<CollectionContainer Collection="{StaticResource mnuResults}" />
<Separator />
<MenuItem Name="mnuFlagContext" Command="local:MainWindow.MarkFlagged"
DataContext="" Visibility="{Binding Path=Flagged, Mode=OneWay,
Converter={StaticResource boolToCollapsedVisibilityConverter}}" />
<!-- I would like to set the DataContext of this one, so it could
be hidden based on a property of the underlying ItemGroup or
ItemType in the TreeView -->
<CollectionContainer Collection="{StaticResource mnuStandardEdit}" />
</CompositeCollection>
</ContextMenu.ItemsSource>
</ContextMenu>
</Window.Resources>
使用上述上下文菜单的 TreeView:
<TreeView Name="myTreeView" DataContext="{Binding ElementName=mainWindow,
Path=RootElement}" ItemsSource="{Binding}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type logic:ItemGroup}"
ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}" Foreground="Blue"
ContextMenu="{Binding Source={StaticResource mnuContextTreeView}}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type logic:ItemType}">
<TextBlock Text="{Binding Name}" Foreground="Red"
ContextMenu="{Binding Source={StaticResource mnuContextTreeView}}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
如何设置名为 mnuFlagContext 的 MenuItem 的 DataContext,以便可以根据 TreeView 中基础 ItemGroup 或 ItemType 的属性隐藏它?