设置在Window.Resources中声明为静态资源的MenuItem的DataContext

时间:2013-06-11 08:58:35

标签: c# wpf xaml datacontext menuitem

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

2 个答案:

答案 0 :(得分:1)

因此,如果要求获取Flagged的{​​{1}} DataContext属性TreeViewItem中的MenuItem.Header

你可以尝试:

ContextMenu

并且原始<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

答案 1 :(得分:0)

使用以下方法管理解决它(最终绑定了Header而不是Visibility,但与解决方案无关):

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;