執行此操作的一種方法是在與HierarchicalDataTemplates進行數據綁定之前讀取XML並將其轉換爲對象。
請注意,在下面的代碼中,我沒有做太多的錯誤檢查。我將你的XML直接複製到XMLFile1.xml中。
MainWindow.xaml.cs:
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<SubSection> SubSections { get; set; }
}
public class SubSection
{
public Attributes Attributes { get; set; }
public List<Item> 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; }
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
LoadFile();
DataContext = this;
}
public List<Section> 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<SubSection>(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<Item>(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<Section>(q);
}
}
}
中XAML文件(MainWindow.xaml):
<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">
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate DataType = "{x:Type src:Section}"
ItemsSource = "{Binding Path=SubSections}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Attributes}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType = "{x:Type src:SubSection}"
ItemsSource = "{Binding Path=Items}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Attributes}"/>
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType = "{x:Type src:Item}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Attributes}"/>
<TextBlock Text=" " />
<TextBlock Text="{Binding Path=Description}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Path=Formula}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Path=Units}" />
</StackPanel>
</DataTemplate>
</Grid.Resources>
<TreeView ItemsSource="{Binding Sections}" />
</Grid>
</Window>
您應該看到這樣的事情:
你可以詳細瞭解MSDN上的分層數據模板。