WPF, display attribute value in TreeView instead of node name

If I have the following data template for TreeView, what do I need to change so that each TreeViewItemshows the attribute value namefor each XML node instead of 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>
+3
source share
2 answers

Nothing, I just had to replace Path=Nameit Path=Valuewith XPath=@nametwo Setters.

+2
source

Replace the binding as follows:

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

Found the answer to this question .

+2
source

Source: https://habr.com/ru/post/1758028/


All Articles