0

我有这个上下文菜单,我在 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 的属性隐藏它?

4

2 回答 2

1

因此,如果要求是获取从到可用的Flagged属性DataContextTreeViewItemMenuItem.HeaderContextMenu

你可以试试:

<ContextMenu x:Key="mnuContextTreeView" 
             DataContext="{Binding RelativeSource={RelativeSource Self},
                                   Path=PlacementTarget.DataContext}">
    <ContextMenu.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{StaticResource mnuRun}" />
            <Separator />
            <CollectionContainer Collection="{StaticResource mnuResults}" />
            <Separator />
            <MenuItem Header="{Binding Path=Flagged,
                                       Mode=OneWay, 
                                       Converter={StaticResource flaggedToHeaderConverter}}"   
                      Command="local:MainWindow.MarkFlagged" />
            <CollectionContainer Collection="{StaticResource mnuStandardEdit}" />
        </CompositeCollection>
    </ContextMenu.ItemsSource>
</ContextMenu>

并且没有更改您的原始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>
于 2013-06-11T19:24:49.713 回答
0

设法通过以下方式解决它(最终绑定了标题而不是可见性,但与解决方案无关):

1)将菜单分离成单独的静态资源:

    <collections:ArrayList x:Key="mnuToggleFlag" x:Shared="False">
        <MenuItem Command="local:MainWindow.ToggleFlag" 
            Header="{Binding Path=Flagged, Mode=OneWay, 
            Converter={StaticResource flaggedToHeaderConverter}}" />
    </collections:ArrayList>

2) 从 ContextMenu 引用它:

<ContextMenu x:Key="mnuContextTreeView">
    <ContextMenu.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{StaticResource mnuRun}" />
            <Separator />
            <CollectionContainer Collection="{StaticResource mnuResults}" />
            <Separator />
            <!-- Below is the reference for the new static resource -->
            <CollectionContainer Collection="{StaticResource mnuToggleFlag}" />
            <CollectionContainer Collection="{StaticResource mnuStandardEdit}" />
        </CompositeCollection>
    </ContextMenu.ItemsSource>
</ContextMenu>

3)从后面的代码设置DataContext:

((MenuItem)((ArrayList)Resources["mnuToggleFlag"])[0]).DataContext = _actualItem;
于 2013-06-11T11:43:06.423 回答