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>
TreeView の基になる ItemGroup または ItemType のプロパティに基づいて非表示にできるように、mnuFlagContext という名前の MenuItem の DataContext を設定するにはどうすればよいですか?