Question

I have this context menu, which I use at different DataTemplates of my TreeView.

<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 which uses the above Context Menu:

<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>

How can I set a DataContext of the MenuItem named mnuFlagContext so it could be hidden based on a property of the underlying ItemGroup or ItemType in the TreeView?

Was it helpful?

Solution 2

Managed to solve it with the following (eventually the Header is bound instead of the Visibility, but is irrelevant regarding the solution):

1) Separating the menu into a separate static resource:

    <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) Referencing it from the 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) Setting DataContext from the code behind:

((MenuItem)((ArrayList)Resources["mnuToggleFlag"])[0]).DataContext = _actualItem;

OTHER TIPS

So if the requirement was to get the Flagged property which is available from the DataContext of the TreeViewItem to the MenuItem.Header in the ContextMenu

you can try:

<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>

and no changes to your original TreeView section

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top