XML到WPF树视图 [英] XML to WPF Tree view

查看:137
本文介绍了XML到WPF树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个XML文件



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







我想要树查看shoud嵌套公关根据它的级别operly



例如像这样



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

解决方案

I认为你的XML结构并不是最好的。如果订单未分类,您可能会得到一些错误的结果。



我更喜欢传统的方式,如果它可以改变XML结构:



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









但是......回到你的问题。



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



  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树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆