针对具有分层数据结构的控件设计的,比如说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的更多相关文章

  1. WPF/Silverlight HierarchicalDataTemplate 模版的使用(转)

    上一篇 对Wpf/Silverlight Template 进行了总结,本篇继续上一篇,主要是介绍 HierarchicalDataTemplate 的使用方法.HierarchicalDataTem ...

  2. TreeView —WPF—MVVM—HierarchicalDataTemplate

    摘要:采用HierarchicalDataTemplate数据模板和treeview在MVVM模式下实现行政区划树, 支持勾选.勾选父节点,子节点回全部自动勾选:子节点部分勾选时,父节点半勾选:子节点 ...

  3. WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeView

    Dictionary中的<string, CustomeType>CustomeType是一个集合,将其绑定生成一棵树,树的第一层节点是Dictionary的Key,第二层是Custome ...

  4. WPF TreeView HierarchicalDataTemplate

    原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...

  5. WPF 自定义列表筛选 自定义TreeView模板 自定义ListBox模板

    有很多项目,都有数据筛选的操作.下面提供一个案例,给大家做参考. 左侧是数据源,搜索框加TreeView控件,右侧是ListBox控件.在左侧数据列点击添加数据,然后点击确定,得到所筛选的数据. 下面 ...

  6. wpf TreeView

    <Window x:Class="WpfTutorialSamples.TreeView_control.TreeViewDataBindingSample"        ...

  7. 【转载】关于treeview的多层显示的科学用法!

    http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-b ...

  8. {二逼小青年的记事簿}为什么treelist不会显示子节点的文字?

    <TreeView Name="treeView" DockPanel.Dock="Left" MinWidth="200" > ...

  9. WPF根据Oracle数据库的表,生成CS文件小工具

    开发小工具的原因: 1.我们公司的开发是客户端用C#,服务端用Java,前后台在通讯交互的时候,会用到Oracle数据库的字段,因为服务器端有公司总经理开发的一个根据Oracle数据库的表生成的cla ...

随机推荐

  1. 用JavaScript和CSS实现“在页面中水平和垂直居中”的时钟

    思路:实现起来最麻烦的事实上是水平居中和垂直居中,当中垂直居中是最麻烦的. 考虑到浏览器兼容性,网上看了一些资料,发如今页面中垂直居中确实没有什么太好的办法. 于是就採用了position:fixed ...

  2. SendMessageTimeout 的使用

    在WINDOW编程中,发送消息的常用API有SendMessage,PostMessage,PostThreadMessage. 一般每个线程有两个队列:一个用来接收通过Send函数的消息,另外一个队 ...

  3. js进阶 11-4/5 jquery中css的类的操作有哪些

    js进阶 11-4/5  jquery中css的类的操作有哪些 一.总结 一句话总结:jquery中css的类的操作有增删切三种. 1.jquery中css的类的操作有哪些? 增删切三种 addCla ...

  4. 断言(Assert)与异常(Exception)

    ## 断言和异常 断言是用来检查非法情况而不是错误情况的,用来帮开发者快速定位问题的位置. 异常处理用于对程序发生异常情况的处理,增强程序的健壮性和容错性. ## 断言的使用 在防御式编程中经常会用断 ...

  5. show binlog events 命令查看某个binlog日志内容

    mysql> show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];   选项解析:   IN 'l ...

  6. 小强的HTML5移动开发之路(25)—— AppCan开发环境

    一.工具下载: 官方下载地址:http://www.appcan.cn/experience.html 按照安装指导默认安装即可 二.建立项目 启动AppCan SDK 建立项目 填入在appcan官 ...

  7. 前端css实现最基本的时间轴

    原型: 图片.png 代码: <!DOCTYPE html > <html> <head> <link rel="stylesheet" ...

  8. Qt多线程和GUI界面假死(run()是线程的入口,就像main()对于应用程序的作用。分析QThread::exec函数的源码,旧的QMutexLocker模式其实很好用,挡住别人进入抢占资源,可照抄)good

    QThread的常见特性: run()是线程的入口,就像main()对于应用程序的作用.QThread中对run()的默认实现调用了exec(),从而创建一个QEventLoop对象,由其处理该线程事 ...

  9. 城市三级联动 AJAX-原生js封装

    话不多说我们先来一张效果图给大家看一下: html代码如下: <!DOCTYPE html><html lang="en"><head> < ...

  10. 检测鼠标指针的改变(使用GetCursorInfo API函数)

    第一步:定义全局变量用于状态改变时的对比 var Form1: TForm1; OldCI:HICON; 第二步:添加Timer组件,Interval设置随意一般50就可以了. 增加Memo组件用于记 ...