WPF 中没有 RootNode 的 TreeView
我有一个 HierarchicalDataTemplate,它是我的 TreeView 的 ItemSource。当 TreeView 显示数据时,他有一个根节点。如何删除根节点?
HierarchicalDataTemplate:
<Window.Resources>
<HierarchicalDataTemplate DataType="cards" ItemsSource="{Binding XPath=child::node()}">
<TextBlock Text="Root"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="category" ItemsSource="{Binding XPath=child::node()}">
<TextBlock Text="{Binding XPath=@name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="card">
<TextBlock Text="{Binding XPath=./title}" />
</HierarchicalDataTemplate>
<XmlDataProvider x:Key="dataxml" XPath="root/cards" />
</Window.Resources>
TreeView:
<TreeView Name="treeViewCategory" ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}"/>
XML:
<root>
<cards>
<category name="Categoryname">
<card>
<title>something</title>
...
.
</card>
<category name="SubCategory">
<card>
<title>something else</title>
..
...
</card>
</category>
</category>
<card>
<title>text</title>
...
..
</card>
</cards>
</root>
实际视图:
o Root
o Categoryname
- something
o SubCategory
- something else
- text
应该是这样:
o Categoryname
- something
o SubCategory
- something else
- text
I have a HierarchicalDataTemplate that is the ItemSource of my TreeView. When the TreeView displays the data he has a rootnode. How can i remove the rootnode?
HierarchicalDataTemplate:
<Window.Resources>
<HierarchicalDataTemplate DataType="cards" ItemsSource="{Binding XPath=child::node()}">
<TextBlock Text="Root"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="category" ItemsSource="{Binding XPath=child::node()}">
<TextBlock Text="{Binding XPath=@name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="card">
<TextBlock Text="{Binding XPath=./title}" />
</HierarchicalDataTemplate>
<XmlDataProvider x:Key="dataxml" XPath="root/cards" />
</Window.Resources>
TreeView:
<TreeView Name="treeViewCategory" ItemsSource="{Binding Source={StaticResource dataxml}, XPath=.}"/>
XML:
<root>
<cards>
<category name="Categoryname">
<card>
<title>something</title>
...
.
</card>
<category name="SubCategory">
<card>
<title>something else</title>
..
...
</card>
</category>
</category>
<card>
<title>text</title>
...
..
</card>
</cards>
</root>
actual view:
o Root
o Categoryname
- something
o SubCategory
- something else
- text
as it should be:
o Categoryname
- something
o SubCategory
- something else
- text
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您分配 TreeView 本身的 ItemsSource 时,只需更进一步,以便根的子项成为树视图的项目。
Just go one step deeper when you assign the ItemsSource of the TreeView itself so that the children of your root become the items for the tree view.
thx HB 完美配合:
我一直试图更改 XmlDataProvider 的源。没想到我应该更改 TreeView 的路径:-(
为什么这很重要?
thx H.B. works perfect with:
i have always tried to change the source of the XmlDataProvider. haven´t imagine that i should change the Path at the TreeView :-(
why does it matter?