Recursive HierarchicalDataTemplate (WPF)

杀马特。学长 韩版系。学妹 提交于 2019-12-21 04:57:15

问题


I'm not sure how to approach this: I want a TreeView that will display some simple data from a hierarchical data structure. As a basic example (In XML, cause it's easy to type):

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

The catch is that this could theoretically nest infinitely deep, so you can't statically define x number of levels and be done with it. Is there a way to define a HierarchicalDataTemplate that can account for this kind of structure?


回答1:


HeirarchicalDataTemplate is used exactly to solve this kind of issue. You can just use a simple template like bellow to achieve this.

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


来源:https://stackoverflow.com/questions/310666/recursive-hierarchicaldatatemplate-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!