Search code examples
wpfxamldata-bindinghierarchicaldatatemplaterelativesource

Bind to DataContext of TreeView from HierachicalDataTemplate


I have a TreeView which contains items populated by a HierarchicalDataTemplate. I am trying to get to a property in the TreeView's DataContext from inside the HierarchicalDataTemplate. Can someone help? Here is what I tried in the HierarchicalDataTemplate:

<HierarchicalDataTemplate x:Key="MyTopLevel"
                                  ItemTemplate="{StaticResource LowerLevelTemplate}"
                                  ItemsSource="{Binding LowerLevel}">
    <TextBlock Text="{Binding Name, Mode=OneWay}" ToolTip="{Binding Name, Mode=OneWay}">
       <TextBlock.ContextMenu>
            <ContextMenu x:Name="MyContextMenu">    
                <MenuItem Header="{Binding DataContext.Test, RelativeSource={RelativeSource AncestorType={x:Type TreeView}}}" />
            </ContextMenu>
        </TextBlock.ContextMenu>
    </TextBlock>
</HierarchicalDataTemplate>

Solution

  • You can use the Tag of the TextBlock to reference the TreeView's DataContext, then you can get it inside the ContextMenu using a relative source binding via the PlacementTarget, e.g.:

    <TextBlock Text="{Binding Name, Mode=OneWay}" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=TreeView}}">
        <TextBlock.ContextMenu>
            <ContextMenu DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
                <MenuItem Header="{Binding Test}"/>
            </ContextMenu>
        </TextBlock.ContextMenu>
    </TextBlock>
    

    If you want to retain the original DataContext of the context menu you can directly navigate to the properties using a full path binding, e.g.:

    <TextBlock Text="{Binding Name, Mode=OneWay}" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=TreeView}}">
        <TextBlock.ContextMenu>
            <ContextMenu>
                <MenuItem Header="{Binding PlacementTarget.Tag.Test, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
            </ContextMenu>
        </TextBlock.ContextMenu>
    </TextBlock>