tags:

views:

42

answers:

2

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>
+1  A: 

Replace your binding with this:

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

Found the answer in this question.

Joseph Sturtevant
Changed `Name` to `name` and that works, thanks!
Sarah Vessels
+1  A: 

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

Sarah Vessels