Here is a sample XML and how it gets bound to a tree. The XML is of the form:
CATEGORY
——REPORT
—–CATEGORY
———-REPORT
——CATEGORY
———–CATEGORY
—————–REPORT
See how there can be any number of Category levels and the Reports can appear at any level too.
Here is the sample code to bind this to the tree: (For better code formatting take a look at this incident in the MSDN Forums: A xml string…need to place nodes in a treeview.
[XAML]
<Window.Resources>
<XmlDataProvider x:Key='myReportsData' XPath='CATEGORY' IsInitialLoadEnabled='True'>
<x:XData>
<CATEGORY NAME='main 1' xmlns=''>
<CATEGORY NAME='sub main 1'>
<REPORT NAME='report111'>
</REPORT>
</CATEGORY>
<CATEGORY NAME='test2222'>
<CATEGORY NAME='test3333'>
<REPORT NAME='report_test222'>
</REPORT>
</CATEGORY>
<CATEGORY NAME='test555'>
<REPORT NAME='report_test333'>
</REPORT>
</CATEGORY>
</CATEGORY>
</CATEGORY>
</x:XData>
</XmlDataProvider>
<HierarchicalDataTemplate DataType='CATEGORY' ItemsSource ='{Binding XPath=*}'>
<TextBlock Text='{Binding XPath=@NAME}' />
</HierarchicalDataTemplate>
<DataTemplate DataType='REPORT'>
<TextBlock Text='{Binding XPath=@NAME}' />
</DataTemplate>
</Window.Resources>
<TreeView Name='myTreeView' Background='Aqua' >
<TreeView.ItemsSource>
<Binding Source='{StaticResource myReportsData}'/>
</TreeView.ItemsSource>
</TreeView>
Share with