Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>EDIT</strong>: Didn't see that you wanted to do this in XAML only. <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cbdb2420-1403-436f-aa7f-b1e3b1acb398" rel="nofollow noreferrer">This</a> MSDN post should help you out.</p> <p><code>HierarchicalDataTemplate</code> isn't so bad if your data is already in a hierarchical form. Let's say you translate that XML data into a model class using code like this:</p> <pre><code>public partial class TreeViewHierarchy : Window { public ObservableCollection&lt;Folder&gt; Folders { get; set; } public TreeViewHierarchy() { Folder system32 = new Folder() { Name = "system32" }; Folder windows = new Folder() { Name = "windows", Children = new ObservableCollection&lt;Folder&gt;() { system32 } }; Folder things = new Folder() { Name = "things" }; Folder stuff = new Folder() { Name = "stuff" }; Folder c = new Folder() { Name = "C:", Children = new ObservableCollection&lt;Folder&gt;() { stuff, things, windows } }; Folders = new ObservableCollection&lt;Folder&gt;() { c }; InitializeComponent(); } } public class Folder { public string Name { get; set; } public ObservableCollection&lt;Folder&gt; Children { get; set; } } </code></pre> <p>Then the XAML for your <code>TreeView</code> would be as simple as this:</p> <pre><code>&lt;Window x:Class="TestWpfApplication.TreeViewHierarchy" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TreeViewHierarchy" Height="300" Width="300" DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt; &lt;TreeView ItemsSource="{Binding Folders}"&gt; &lt;TreeView.ItemTemplate&gt; &lt;HierarchicalDataTemplate ItemsSource="{Binding Children}"&gt; &lt;TextBlock Text="{Binding Name}"/&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/TreeView.ItemTemplate&gt; &lt;/TreeView&gt; </code></pre> <p></p> <p>And the result:</p> <p><a href="http://img191.imageshack.us/img191/7841/treeviewhierarchy.png" rel="nofollow noreferrer">alt text http://img191.imageshack.us/img191/7841/treeviewhierarchy.png</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload