i have the following xml structure and want to add it to a treeview. below is my first test.
my problem is that i don´t know how to bind the list to the treeview because the childelements must consists of poth elements (category & card).
-cards
-category
-card
-card
-category
-card
-card
-card
<Window.Resources>
<HierarchicalDataTemplate DataType="cards" ItemsSource="{Binding XPath=card}">
<TextBlock Text="My Cards" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="category" ItemsSource="{Binding XPath=card}">
<TextBlock Text="{Binding XPath=@name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="card" ItemsSource="{Binding XPath=category}">
<TextBlock Text="{Binding XPath=@name}"/>
</HierarchicalDataTemplate>
<XmlDataProvider x:Key="dataxml" XPath="cards" Source="folder\cards.xml" />
</Window.Resources>
<TreeView Name="treeView" ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}" />
Sample of the XML:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<settings>
....
..
</settings>
<cards>
<category name="C1">
<card name="card1">
<question>bla</question>
<answer>blub</answer>
</card>
<category name="C2">
<card name="card4">
<question>bla</question>
<answer>blub</answer>
</card>
</category>
</category>
<card name="card2">
<question>bla</question>
<answer>blub</answer>
</card>
<card name="card3">
<question>bla</question>
<answer>blub</answer>
</card>
</cards>
</root>
A generic approach can be done like this; which IMHO is a nice approach for the data if it is indeed arbitrary.
The
TreeViewwould then look like this…If the data is not arbitrary I would suggest parsing the XML via LINQ or other method and moving the data into a composite type which would could then be bound to via the
HierarchicalDataTemplatein a more granular fashion. If you were to go that route a great walk through should suffice in achieving what you are after.