XML到WPF树视图

编程入门 行业动态 更新时间:2024-10-07 20:33:33
本文介绍了XML到WPF树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嗨我有一个XML文件

< TreeView > < Level1 类别 = 北美 / > < Level2 类别 = 美国 / > < Level3 C ategory = 加拿大 / > < Level2 类别 = 墨西哥 / > < Level1 类别 = 南美洲 / > < Level2 类别 = 阿根廷 / > < Level2 类别 = 巴西 / > < ; Level2 类别 = 乌拉圭 / > < / TreeView >

我想要树查看shoud嵌套公关根据它的级别operly 例如像这样

< TreeView 名称 = 电视 > < TreeViewItem 标题 = 北美 > < TreeViewItem 标题 = 美国 > < TreeViewItem 标题 = 加拿大 > < / TreeViewItem > < / TreeViewItem > < TreeViewItem 标题 = 墨西哥 > < / TreeViewItem > < / TreeViewItem > < TreeViewItem 标题 = 南美洲 > < TreeViewItem 标题 = 阿根廷 > < / TreeViewItem > < TreeViewItem 标题 = 巴西 > < / TreeViewItem > < TreeViewItem 标题 = 乌拉圭 > < / TreeViewItem > < / TreeViewItem > < / TreeView >

解决方案

I认为你的XML结构并不是最好的。如果订单未分类,您可能会得到一些错误的结果。 我更喜欢传统的方式,如果它可以改变XML结构:

< 级别 > < 等级 类别 = Northamerica > < 级别 类别 = 美国 / > < 级别 类别 = 加拿大 / > < / Level > <! - 等等... - > < 级别 >

但是......回到你的问题。 首先你需要上课你的物品。每个项目都可能有一些子项目。所以我们需要一个属性以及该类别的属性。

Public 类 LevelItem 公共 属性 InnerItems 作为 新列表( LevelItem) 公共 属性类别作为 字符串 结束 Class

您需要将XML解析为代码隐藏或ViewModel中的某些数据。 /> 您的XML解析器必须生成xml中的项目顺序,并且应该返回List(Of ListItem)或类似的东西。此列表是TreeView的ItemsSource。将它存放在一个属性或quickWay中:

我 .treeViewData.Itemssource = YourXMLParser.ParseXML( PATH)

在XAML中你也需要一些东西: 首先在Window / UserControl的资源中创建一个HierarchicalDataTemplate,如下所示:

< Window.Resources > < HierarchicalDataTemplate x:键 = LevelItemTemplate ItemsSource = {Binding Path = InnerItems} > < TextBlock 文字 = {Binding Category} / > < / HierarchicalDataTemplate > < / Window.Resources >

最后你需要将模板分配给你的TreeView:

< TreeView 名称 = lvData ItemTemplate = {StaticResource LevelItemTemplate} / >

就是这样! :)

Hi I have a XML file

<TreeView> <Level1 Category="North America"/> <Level2 Category="USA"/> <Level3 Category="Canada"/> <Level2 Category="Mexico"/> <Level1 Category="South America"/> <Level2 Category="Argentina"/> <Level2 Category="Brazil"/> <Level2 Category="Uruguay"/> </TreeView>

I want it the tree view shoud nested properly according to its Level For eg like this

<TreeView Name="TV"> <TreeViewItem Header="North America"> <TreeViewItem Header="USA"> <TreeViewItem Header="Canada"></TreeViewItem> </TreeViewItem> <TreeViewItem Header="Mexico"></TreeViewItem> </TreeViewItem> <TreeViewItem Header="South America"> <TreeViewItem Header="Argentina"></TreeViewItem> <TreeViewItem Header="Brazil"></TreeViewItem> <TreeViewItem Header="Uruguay"></TreeViewItem> </TreeViewItem> </TreeView>

解决方案

I think your XML-structure isnot really the best. If the order is unsorted, you might get some wrong results. I would prefer the traditonal way, if its possible to change the XML-structure :

<Levels> <Level Category="Northamerica"> <Level Category="USA"/> <Level Category="Canada"/> </Level> <!-- and so on ... --> <Levels>

But... Back to your question. First you need a class for your item. Each item could has some childitems. So we need a property for this and a property for the category.

Public Class LevelItem Public Property InnerItems As New List(Of LevelItem) Public Property Category As String End Class

You need to parse your XML to some data in your codebehind or ViewModel. Your XML-parser must generate the order of the items as in the xml and should return a List(Of ListItem) or something like that. This list is the ItemsSource of your TreeView. Store it in a property, or the quickWay :

Me.treeViewData.Itemssource = YourXMLParser.ParseXML("PATH")

In XAML you need some things too : First create a HierarchicalDataTemplate in the resources of your Window/UserControl as following :

<Window.Resources> <HierarchicalDataTemplate x:Key="LevelItemTemplate" ItemsSource="{Binding Path=InnerItems}"> <TextBlock Text="{Binding Category}"/> </HierarchicalDataTemplate> </Window.Resources>

Finally you need to allocate the Template to your TreeView :

<TreeView Name="lvData" ItemTemplate="{StaticResource LevelItemTemplate}"/>

That it is! :)

更多推荐

XML到WPF树视图

本文发布于:2023-11-12 20:48:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582495.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   XML   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!