HierarchicalDataTemplate
针对具有分层数据结构的控件设计的,比如说TreeView,相当于可以每一个层级上做DataTemplate
XmlDataProvider:数据源,写在Resources下
<XmlDataProvider x:Key="Info" XPath="Nations"> <x:XData> <Nations xmlns=""> <Nation Name="中国"> <Provinces> <Province Name="安徽"> <Citys> <City Name="安庆"> <Countrys> <Country Name="潜山"/> <Country Name="桐城"/> </Countrys> </City> <City Name="合肥"> <Countrys> <Country Name="长丰"/> <Country Name="肥东"/> </Countrys> </City> </Citys> </Province> <Province Name="江苏"> <Citys> <City Name="南京"> <Countys> <Country Name="溧水"/> <Country Name="高淳"/> </Countys> </City> <City Name="苏州"> <Countys> <Country Name="常熟"/> </Countys> </City> </Citys> </Province> </Provinces> </Nation> </Nations> </x:XData> </XmlDataProvider>
HierarchicalDataTemplate:层级模板,写在Resources下
<HierarchicalDataTemplate DataType="Nation" ItemsSource="{Binding XPath=Provinces/Province}"><StackPanel Background="AliceBlue"> <TextBlock FontSize="20" Text="{Binding XPath=@Name}"/> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="Province" ItemsSource="{Binding XPath=Citys/City}"> <StackPanel Background="LightBlue"> <TextBlock FontSize="18" Text="{Binding XPath=@Name}"/> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="City" ItemsSource="{Binding XPath=Countrys/Country}"> <StackPanel Background="LightBlue"> <TextBlock FontSize="18" Text="{Binding XPath=@Name}"/> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="Country"> <StackPanel Background="LightSalmon"> <TextBlock FontSize="18" Text="{Binding XPath=@Name}"/> </StackPanel> </HierarchicalDataTemplate>
解释
<HierarchicalDataTemplate DataType="Nation" ItemsSource="{Binding XPath=Provinces/Province}"><StackPanel Background="AliceBlue"> <TextBlock FontSize="20" Text="{Binding XPath=@Name}"/> </StackPanel> </HierarchicalDataTemplate>
DataType表示定义的目标是Nation
ItemsSource表示下一级是Provinces/Province (总标签/单个标签名)
StackPanel 定义Nation的外观
XPath=@Name表示绑定为Name属性
比如:
<Nation Name="中国" Age="15"><HierarchicalDataTemplate DataType="Nation" ItemsSource="{Binding XPath=Provinces/Province}"> <StackPanel Background="AliceBlue"> <TextBlock FontSize="20" Text="{Binding XPath=@Name}"/> <Label FontSize="15" Content="{Binding XPath=@Age}"></Label> </StackPanel> </HierarchicalDataTemplate> </Nation>
![](https://upload-images.jianshu.io/upload_images/9128511-203e676c2862e98d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/525)
TreeView
<TreeView ItemsSource="{Binding Source={StaticResource ResourceKey=Info},XPath=Nation}"></TreeView>
像引用静态资源一样使用
XPath决定显示的根节点
如果想从第二/三级开始显示,而不是根节点
修改XPath(写路径,否则找不到)
<TreeView ItemsSource="{Binding Source={StaticResource ResourceKey=Info},XPath=Nation/Provinces/Province}"></TreeView>
![](https://upload-images.jianshu.io/upload_images/9128511-e52ddf49f0a463d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/525)
<TreeView ItemsSource="{Binding Source={StaticResource ResourceKey=Info},XPath=Nation/Provinces/Province/Citys/City}"></TreeView>
![](https://upload-images.jianshu.io/upload_images/9128511-0080a43b111334de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/525)
转载于:https://www.cnblogs.com/Lulus/p/8157718.html
HierarchicalDataTemplate相关推荐
- 详解Silverlight Treeview的HierarchicalDataTemplate使用
在Silverlight项目中,Treeview控件是比较常用的表示层次或者等级的控件,该控件可以非常清晰的显示数据之间的隶属关系.对于Treeview控件的基本使用已经有很多文章介绍,这里我想讲解一 ...
- WPF TreeView HierarchicalDataTemplate
原文 WPF TreeView HierarchicalDataTemplate <StackPanel Margin="0,0,0,0"><StackPan ...
- HierarchicalDataTemplate中的ContextMenu的Command绑定
<ContextMenu x:Key="ModeMenu"> <MenuItem Header="添加" Command="{Bin ...
- WPF疑难问题之Treeview中HierarchicalDataTemplate多级样式
文章目录 一.问题场景 二.解决思路 一.问题场景 日常为 TreeView 自定义样式过程中,如果涉及到树形多级样式不同时,又该如何去做?例如树形显示文件夹和文件节点. TreeView 样式如下: ...
- WPF/Silverlight HierarchicalDataTemplate 模版的使用
上一篇 对Wpf/Silverlight Template 进行了总结,本篇继续上一篇,主要是介绍 HierarchicalDataTemplate 的使用方法.HierarchicalDataTem ...
- WPF/Silverlight HierarchicalDataTemplate 模版的使用(转)
上一篇 对Wpf/Silverlight Template 进行了总结,本篇继续上一篇,主要是介绍 HierarchicalDataTemplate 的使用方法.HierarchicalDataTem ...
- WPF中触发器Trigger、MultiTrigger、DataTrigger、MultiDataTrigger、EventTrigger几种
WPF中有种叫做触发器的东西(记住不是数据库的trigger哦).它的主要作用是根据trigger的不同条件来自动更改外观属性,或者执行动画等操作. WPFtrigger的主要类型有:Trigger. ...
- 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据...
目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中一种绑定方法,后面多几列其他属性都没关系,例如多个字段, 1 A 0 2 B 0 3 C 0 4 D 1 ...
- 使用Silverlight Toolkit TreeView(树形控件)
尽管在Silverlight Toolkit中有相关的DEMO来演示如何使用TreeView控件,但其还是有一些功能没被演示出来.因为在我们平时开发过程中,数据是被动态查询获取的(不是DEMO中 ...
最新文章
- 操作系统安全加固方法
- 关于checkbox
- SignalR的性能监测与服务器的负载测试
- php使用ftp远程上传文件类(解决主从文件同步问题的简单方法)
- 计算机绘图图库的创建,工程制图与计算机绘图.doc
- SEO优化:WordPress发布文章主动推送到百度,加快收录保护原创
- 苹果手机夜间模式怎么设置_微信怎么设置夜间模式?iPhone夜间模式设置教程 省电又护眼!...
- 使用 Shell 脚本实现安装进度指示器
- 数据--第44课 - 最小连通网
- CDH-CM资源下载
- STM32中大小端转换
- 树莓派cm4安装ax200驱动-wifi6
- Ubuntu-安装输入法
- 将多个csv文件合并成一个
- CorelDraw 13从入门到精通视频教程
- append()与prepend()的区别
- asp.net网站修改aspx.cs文件后如何不替换网站就生效
- 温故而知新的知识蒸馏 Distilling Knowledge
- java坦克大战 实训报告_坦克大战系统《Java程序开发实训》综合实训报告.doc
- (萌新的数电学习)用VHDL语言设计CPU