3

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>

2 Answers 2

2

Replace your binding with this:

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

Found the answer in this question.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.