Recursive Hierarchy Table (WPF)

I'm not sure how to approach this: I want the TreeView to display some simple data from a hierarchical data structure. As a basic example (in XML, it's easy to introduce):

<Node text="Root">
    <Node text="Item 1">
        <Node text="Item 1.1" />
    </Node>
    <Node text="Item 2"/>
</Node>

The trap is that this can theoretically be infinitely deep, so you cannot statically determine x the number of levels and do with it. Is there a way to define a HierarchicalDataTemplate that can take such a structure into account?

+5
source share
1 answer

HeirarchicalDataTemplate is used precisely to solve this problem. You can use a simple template like below to achieve this.

  <HierarchicalDataTemplate DataType="Node" ItemsSource ="{Binding XPath=*}">
        <TextBlock Text="{Binding XPath=@text}" />
    </HierarchicalDataTemplate>
+11
source

All Articles