将 XML 多重绑定到 TreeView
我有以下 xml 结构并想将其添加到树视图中。下面是我的第一次测试。 我的问题是我不知道如何将列表绑定到树视图,因为子元素必须由 poth 元素(类别和卡片)组成。
-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=.}" />
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通用方法可以像这样完成;恕我直言,如果数据确实是任意的,那么这对于数据来说是一个很好的方法。
TreeView 看起来像这样...
如果数据不是任意的,我建议通过 LINQ 或其他方法解析 XML,并将数据移动到复合类型中,然后可以通过以更细粒度的方式构建
HierarchicalDataTemplate
。如果您要走这条路线 伟大的演练应该足以实现你所追求的目标。A generic approach can be done like this; which IMHO is a nice approach for the data if it is indeed arbitrary.
The
TreeView
would 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
HierarchicalDataTemplate
in a more granular fashion. If you were to go that route a great walk through should suffice in achieving what you are after.