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>

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>

<TreeView ItemsSource="{Binding Source={StaticResource ResourceKey=Info},XPath=Nation/Provinces/Province/Citys/City}"></TreeView>

HierarchicalDataTemplate的更多相关文章
- WPF/Silverlight HierarchicalDataTemplate 模版的使用(转)
上一篇 对Wpf/Silverlight Template 进行了总结,本篇继续上一篇,主要是介绍 HierarchicalDataTemplate 的使用方法.HierarchicalDataTem ...
- TreeView —WPF—MVVM—HierarchicalDataTemplate
摘要:采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树, 支持勾选.勾选父节点,子节点回全部自动勾选:子节点部分勾选时,父节点半勾选:子节点 ...
- WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeView
Dictionary中的<string, CustomeType>CustomeType是一个集合,将其绑定生成一棵树,树的第一层节点是Dictionary的Key,第二层是Custome ...
- WPF TreeView HierarchicalDataTemplate
原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...
- WPF 自定义列表筛选 自定义TreeView模板 自定义ListBox模板
有很多项目,都有数据筛选的操作.下面提供一个案例,给大家做参考. 左侧是数据源,搜索框加TreeView控件,右侧是ListBox控件.在左侧数据列点击添加数据,然后点击确定,得到所筛选的数据. 下面 ...
- wpf TreeView
<Window x:Class="WpfTutorialSamples.TreeView_control.TreeViewDataBindingSample" ...
- 【转载】关于treeview的多层显示的科学用法!
http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-b ...
- {二逼小青年的记事簿}为什么treelist不会显示子节点的文字?
<TreeView Name="treeView" DockPanel.Dock="Left" MinWidth="200" > ...
- WPF根据Oracle数据库的表,生成CS文件小工具
开发小工具的原因: 1.我们公司的开发是客户端用C#,服务端用Java,前后台在通讯交互的时候,会用到Oracle数据库的字段,因为服务器端有公司总经理开发的一个根据Oracle数据库的表生成的cla ...
随机推荐
- Android shape画圆点
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...
- Windows Phone 8.1 控件
如果你已经开始了 Windows Phone 8.1 的学习,就会发现许多在 8.0 下的控件在 8.1 中都发生了变化,以下就谈谈几个 8.1 下的新控件以及与 8.0 控件的改变. 1. Text ...
- 学习鸟哥的Linux私房菜笔记(12)——系统监视2
四.控制进程 kill :语法 kill [-signal] PID 向进程传送一个特定的讯号,默认为15(终结) kill -l :列出所有可以由kill传递的讯号 1 :重启进程 2 : ...
- 使用RpcLite构建SOA/Web服务(Full .Net Framework)
使用RpcLite构建SOA/Web服务(Full .Net Framework) SOA框架系列 1. 使用RpcLite构建SOA/Web服务 2. 使用RpcLite构建SOA/Web服务(Fu ...
- 【codeforces 782C】Andryusha and Colored Balloons
[题目链接]:http://codeforces.com/contest/782/problem/C [题意] 给你一棵树 让你满足要求 ->任意相连的3个节点的颜色不能相同 的情况下进行染色 ...
- Android菜鸟的成长笔记(20)——IntentService
前面介绍的Service在官方文档介绍中说Service存在着如下两个问题: 1.A Service is not a separate process. The Service object its ...
- 同一性(identical)
f(x)=x,表明 f(⋅) 为同一函数. A 与 B 具有 360° 的区别 A 向左转,再向右转 ⇒ A A 向左转,向左转,向后转 ⇒ A
- Sql 将多个表查询的结果进行再次查询
把你目前查到结果集定义为一个临时表 tempTable 下面是如何查 SELECT * FROM tempTable where 关键字=‘’举例 select book_num,book_name, ...
- TCL S960T刷机包 乐蛙OS5 稳定版 平滑 优化
ROM简介 乐蛙OS5完美的最终稳定版 Ver14.10.17 温馨提示:一定要明确系统双成一个完整的包画刷入前开发版,否则会造成系统异常,请务必备份手机刷机前的信息和数据,刷机过程中,为了避免因数据 ...
- 一:redis 的string类型 - 相关操作
*redisclient使用: =============一类:string的方法================ 介绍:string是redis的最简单类型,一个key相应一个value,strin ...