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>

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

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 に答える