Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One way to do this is to read the XML and convert into objects before doing databinding with HierarchicalDataTemplates.</p> <p>Note that in the code below I don't do much error checking. I copied your XML directly into XMLFile1.xml.</p> <p>MainWindow.xaml.cs:</p> <pre><code>using System.Collections.Generic; using System.Linq; using System.Windows; using System.Xml.Linq; namespace WpfApplication { public class Attributes { public string Ref { get; set; } public string Name { get; set; } public override string ToString() { return Ref + " " + Name; } } public class Section { public Attributes Attributes { get; set; } public List&lt;SubSection&gt; SubSections { get; set; } } public class SubSection { public Attributes Attributes { get; set; } public List&lt;Item&gt; Items { get; set; } } public class Item { public Attributes Attributes { get; set; } public string Description { get; set; } public string Units { get; set; } public string Formula { get; set; } } /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); LoadFile(); DataContext = this; } public List&lt;Section&gt; Sections { get; private set; } private void LoadFile() { XDocument xmlFile = XDocument.Load(@"XMLFile1.xml"); var q = from section in xmlFile.Descendants("Section") select new Section() { Attributes = new Attributes() { Ref = section.Attribute("ref").Value, Name = section.Attribute("name").Value }, SubSections = new List&lt;SubSection&gt;(from subsection in section.Descendants("Subsection") select new SubSection() { Attributes = new Attributes() { Ref = subsection.Attribute("ref").Value, Name = subsection.Attribute("name").Value }, Items = new List&lt;Item&gt;(from item in subsection.Descendants("Item") select new Item() { Attributes = new Attributes() { Ref = item.Attribute("ref").Value, Name = item.Attribute("name").Value }, Description = item.Element("Description").Value, Formula = item.Element("Formula").Value, Units = item.Element("Units").Value }) }) }; Sections = new List&lt;Section&gt;(q); } } } </code></pre> <p>The in the XAML file (MainWindow.xaml):</p> <pre><code>&lt;Window x:Class="WpfApplication.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:WpfApplication" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;Grid.Resources&gt; &lt;HierarchicalDataTemplate DataType = "{x:Type src:Section}" ItemsSource = "{Binding Path=SubSections}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Path=Attributes}"/&gt; &lt;/StackPanel&gt; &lt;/HierarchicalDataTemplate&gt; &lt;HierarchicalDataTemplate DataType = "{x:Type src:SubSection}" ItemsSource = "{Binding Path=Items}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Path=Attributes}"/&gt; &lt;/StackPanel&gt; &lt;/HierarchicalDataTemplate&gt; &lt;DataTemplate DataType = "{x:Type src:Item}"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Path=Attributes}"/&gt; &lt;TextBlock Text=" " /&gt; &lt;TextBlock Text="{Binding Path=Description}" /&gt; &lt;TextBlock Text=" " /&gt; &lt;TextBlock Text="{Binding Path=Formula}" /&gt; &lt;TextBlock Text=" " /&gt; &lt;TextBlock Text="{Binding Path=Units}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Grid.Resources&gt; &lt;TreeView ItemsSource="{Binding Sections}" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>You should see something like this: <img src="https://i.stack.imgur.com/gRsal.png" alt="MainWindow"></p> <p>You can find out more about hierarchical data templates on <a href="http://msdn.microsoft.com/en-us/library/system.windows.hierarchicaldatatemplate.aspx" rel="nofollow noreferrer">MSDN</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