Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF, display attribute value in TreeView instead of node name

Tags:

xml

wpf

xaml

xpath

If I have the following data template for a TreeView, what do I need to change so that each TreeViewItem shows the value of the name attribute on each XML node, instead of the node name?

<HierarchicalDataTemplate x:Key="NodeTemplate">
    <TextBlock x:Name="tb"/>
    <HierarchicalDataTemplate.ItemsSource>
        <Binding XPath="child::node()" />
    </HierarchicalDataTemplate.ItemsSource>
    <HierarchicalDataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
            <Setter TargetName="tb" Property="Text" Value="{Binding Path=Value}"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
            <Setter TargetName="tb" Property="Text" Value="{Binding Path=Name}"/>
        </DataTrigger>
    </HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
like image 537
Sarah Vessels Avatar asked May 31 '26 13:05

Sarah Vessels


2 Answers

Replace your binding with this:

<Setter TargetName="tb" Property="Text" Value="{Binding Path=Attributes[Name].Value}" />

Found the answer in this question.

like image 69
Joseph Sturtevant Avatar answered Jun 02 '26 05:06

Joseph Sturtevant


Neeeeever mind, just had to replace Path=Name and Path=Value with XPath=@name in the two Setters.

like image 44
Sarah Vessels Avatar answered Jun 02 '26 03:06

Sarah Vessels