在Silverlight项目中,Treeview控件是比较常用的表示层次或者等级的控件,该控件可以非常清晰的显示数据之间的隶属关系。对于Treeview控件的基本使用已经有很多文章介绍,这里我想讲解一下Silverlight Treeivew的HierarchicalDataTemplate的使用方法。
HierarchicalDataTemplate可以叫做"层级式数据模板",主要是应用层级比较明显数据集合。下面我来一步步演示HierarchicalDataTemplate在Silverlight treeview中的使用方法。在演示中,我将引用另外一个Silverlight控件ListBox进行对比,因为,Treeview和Listbox都属于Itemscontrol,
所以有很多类似相同之处,通过对比能够帮助大家记忆以及使用该控件。
首先建立一个空的项目, 
 
在MainPage页面中建立一个ListBox,在Xaml中写入代码,
 1 <UserControl x:Class="TreeviewDemo.MainPage"
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
 5     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 6     xmlns:sys="clr-namespace:System;assembly=mscorlib"
 7     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
 8   <Grid x:Name="LayoutRoot">
 9         <ListBox>
10             <sys:String>树型演示1</sys:String>
11             <sys:String>树型演示2</sys:String>
12             <sys:String>树型演示3</sys:String>
13             <sys:String>树型演示4</sys:String>
14             <sys:String>树型演示5</sys:String>
15         </ListBox>
16     </Grid>
17 </UserControl>
运行后会显示:
 
在上面代码基础上,我们可以添加一个ItemTemplate,对数据进行绑定。
 1 <UserControl x:Class="TreeviewDemo.MainPage"
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
 5     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 6     xmlns:sys="clr-namespace:System;assembly=mscorlib"
 7     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
 8   <Grid x:Name="LayoutRoot">
 9         <ListBox>
10             <ListBox.ItemTemplate>
11                 <DataTemplate>
12                     <TextBlock Foreground="Blue" Text="{Binding}" />
13                 </DataTemplate>
14             </ListBox.ItemTemplate>
15             <sys:String>树型演示1</sys:String>
16             <sys:String>树型演示2</sys:String>
17             <sys:String>树型演示3</sys:String>
18             <sys:String>树型演示4</sys:String>
19             <sys:String>树型演示5</sys:String>
20         </ListBox>
21     </Grid>
22 </UserControl>
23 
运行结果如下:
 
这里ListBox的选项都变成了蓝色。
就像我们前面所说的,ListBox是一个ItemsControl,任何ItemsControl都是相同的,可以将它们的内容包括到一个容器中。所以,我们可以再次重写上面代码:
 1 <UserControl x:Class="TreeviewDemo.MainPage"
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
 5     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 6     xmlns:sys="clr-namespace:System;assembly=mscorlib"
 7     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
 8   <Grid x:Name="LayoutRoot">
 9         <ListBox>
10             
11             
12             <ListBoxItem Content="树型演示1">
13                 <ListBoxItem.ContentTemplate>
14                     <DataTemplate x:Name="myTemplate">
15                         <TextBlock Foreground="Blue" Text="{Binding}" />
16                     </DataTemplate>
17                 </ListBoxItem.ContentTemplate>
18             </ListBoxItem>
19             <ListBoxItem Content="树型演示2" ContentTemplate="{Binding ElementName=myTemplate}" />
20             <ListBoxItem Content="树型演示3" ContentTemplate="{Binding ElementName=myTemplate}" />
21             <ListBoxItem Content="树型演示4" ContentTemplate="{Binding ElementName=myTemplate}" />
22             <ListBoxItem Content="树型演示5" ContentTemplate="{Binding ElementName=myTemplate}" />
23         </ListBox>
24     </Grid>
25 </UserControl>
26 
在上面的代码中,ListBox中创建五个ListBoxItem,ListBoxItem的Content属性绑定着不同的选项,而ListBoxItem的ContentTemplate绑定着ListBox的ItemTemplate。
运行结果和上面的相同:
根据上面的基础,我们可以使用同样的概念来理解Silverlight Treeivew控件。
在使用Treeview控件前,需要添加引用,Treeview控件被装配在System.Windows.Controls下,另外在客户端页面需要添加命名空间如下:
xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
Treeview控件也是一个ItemsControl,同样,每次初始化,Treeview控件会为所属选项创建TreeViewItem。 如果我们使用和ListBox同样的代码,可以得到下面结果,
 1 <UserControl x:Class="TreeviewDemo.MainPage"
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
 5     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 6     xmlns:sys="clr-namespace:System;assembly=mscorlib"
 7     xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
 8     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
 9   <Grid x:Name="LayoutRoot">
10         <Controls:TreeView>
11             <sys:String>树形演示1</sys:String>
12             <sys:String>树形演示2</sys:String>
13             <sys:String>树形演示3</sys:String>
14         </Controls:TreeView>
15     </Grid>
16 </UserControl>
17 
运行结果:
同样,也可以添加ItemTemplate到Treeview控件,
 1 <UserControl x:Class="TreeviewDemo.MainPage"
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
 5     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 6     xmlns:sys="clr-namespace:System;assembly=mscorlib"
 7     xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
 8     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
 9   <Grid x:Name="LayoutRoot">
10         <Controls:TreeView>
11             <Controls:TreeView.ItemTemplate>
12                     <DataTemplate>
13                         <TextBlock Foreground="Green" Text="{Binding}" />
14                     </DataTemplate>
15             </Controls:TreeView.ItemTemplate>
16             <sys:String>树型演示1</sys:String>
17             <sys:String>树型演示2</sys:String>
18             <sys:String>树型演示3</sys:String>
19         </Controls:TreeView>
20     </Grid>
21 </UserControl>
22 
运行结果:
从上面,我们可以看出,ListBox和Treeview有很多相似之处,在一些情况下基本可以替换使用,但是,这两个控件也有明显的区别。TreeView控件在建立选项的时候,使用的是TreeViewItem类,而TreeViewItem是HeaderedItemsControl(详细定义可以查看MSDN http://msdn.microsoft.com/en-us/library/system.windows.controls.treeviewitem(VS.95).aspx),作为HeaderedItemsControl,可以将控件选项内容赋值到Header或者HeaderTemplate属性中。这里,我们可以简单的理解,HeaderedItemsControl的Header/HeaderTemplate和ContentControl的Content/ContentTemplate功能是相同的,都是呈现内容的载体。 所以,在ListBox中,选项是被绑定到ListBoxItem的content属性中,而在Treeview控件中,选项是被绑定到TreeViewItem的Header属性中。同样,TreeView的ItemTemplate绑定也可以使用TreeviewItem的HeaderTemplate属性进行绑定,结果是相同的。根据上面所述,可以得到下面的代码:
 1 <UserControl x:Class="TreeviewDemo.MainPage"
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
 5     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
 6     xmlns:sys="clr-namespace:System;assembly=mscorlib"
 7     xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
 8     mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
 9   <Grid x:Name="LayoutRoot">
10         <Controls:TreeView>
11             <Controls:TreeViewItem Header="树型演示1">
12                 <Controls:TreeViewItem.HeaderTemplate>
13                     <DataTemplate x:Name="myTemplate">
14                         <TextBlock Foreground="Green" Text="{Binding}" />
15                     </DataTemplate>
16                 </Controls:TreeViewItem.HeaderTemplate>
17             </Controls:TreeViewItem>
18             <Controls:TreeViewItem Header="树型演示2" HeaderTemplate="{Binding ElementName=myTemplate}" />
19             <Controls:TreeViewItem Header="树型演示3" HeaderTemplate="{Binding ElementName=myTemplate}" />
20         </Controls:TreeView>
21     </Grid>
22 </UserControl>
23 
运行结果和上面相同:
相信通过上面的演示,大家已经基本理解ItemsControl的Template使用,根据上述,我们可以延伸到HierarchicalDataTemplate,使用HierarchicalDataTemplate我们需要建立一个例程数据类供TreeView调用。
 1 public class Country
 2     {
 3         public Country()
 4         {
 5             Privinces = new ObservableCollection<Province>();
 6         }
 7 
 8         public string Name { get; set; }
 9         public ObservableCollection<Province> Privinces { get; set; }
10     }
11 
12 public class Province
13     {
14         public Province()
15         {
16             Citys = new ObservableCollection<City>();
17         }
18 
19         public string Name { get; set; }
20         public ObservableCollection<City> Citys { get; set; }
21     }
22 
23 public class City
24     {
25         public string Name { get; set; }
26     }
然后建立例程数据,代码如下:
代码

 1 tvDemo.ItemsSource = new ObservableCollection<Country> { 
 2             new Country { 
 3                 Name = "中国", 
 4                 Privinces = { new Province 
 5                   { 
 6                     Name="山东省",
 7                     Citys = { 
 8                         new City { Name = "济南市" },
 9                         new City { Name= "淄博市" }
10                     }
11                   },
12                 new Province 
13                   { 
14                     Name="广东省", 
15                     Citys = { 
16                         new City { Name = "广州市" },
17                         new City { Name= "佛山市" }
18                     }
19                   }
20                }
21             },
22             new Country { 
23                 Name = "加拿大", 
24                 Privinces = { new Province 
25                   { 
26                     Name="哥伦比亚省",
27                     Citys = { 
28                         new City { Name = "温哥华市" },
29                         new City { Name= "维多利亚市" }
30                     }
31                   },
32                 new Province 
33                   { 
34                     Name="阿尔伯塔省", 
35                     Citys = { 
36                         new City { Name = "埃德蒙顿市" },
37                         new City { Name= "卡尔加里市" }
38                     }
39                   }
40                }
41             }
42         };

首先我们使用TreeView的ItemTemplate来显示该数据树形结构,前台代码:
1         <Controls:TreeView x:Name="tvDemo">
2             <Controls:TreeView.ItemTemplate>
3                <DataTemplate>
4                         <TextBlock Text="{Binding Name}" />
5                </DataTemplate>
6             </Controls:TreeView.ItemTemplate>
7         </Controls:TreeView>
显示结果如下:
这里Treeview控件建立了两个TreeViewItems,并且绑定TreeViewitem的Header属性到Country对象,而且将TreeViewItem的HeaderTemplate设置为TreeView的ItemTemplate。下面,我们需要子数据同时绑定到Treeview控件中,这里我们需要使用HierarchicalDataTemplate。在使用HierarchicalDataTemplate前,需要声明新的命名空间:
xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
其实HierarchicalDataTemplate是一个带有多个扩展属性DataTemplate。 如果我们不使用这些扩展属性,HierarchicalDataTemplate和普通DataTemplate是相同的,例如,我们修改上面代码:
1 <Controls:TreeView x:Name="tvDemo">
2             <Controls:TreeView.ItemTemplate>
3                 <common:HierarchicalDataTemplate>
4                     <TextBlock Text="{Binding Name}" />
5                 </common:HierarchicalDataTemplate>
6             </Controls:TreeView.ItemTemplate>
7 </Controls:TreeView>
显示结果和上面相同:
所谓HierarchicalDataTemplate的扩展属性,主要是ItemsSource和ItemTemplate两个属性。其中ItemsSource属性可以获取TreeView.ItemsSource的数据,ItemTemplate可以获取到TreeViewItem.ItemTemplate模板。根据这两个属性,我们可以修改以上代码,获取到子数据。通常来说,我们会把HierarchicalDataTemplate定义在Resource中,这样可以使代码布局整洁,另外提高易读性。
 1     <UserControl.Resources>
 2         <common:HierarchicalDataTemplate x:Key="CityTemplate">
 3             <StackPanel>
 4                 <TextBlock Text="{Binding Name}"/>
 5             </StackPanel>
 6         </common:HierarchicalDataTemplate>
 7         <common:HierarchicalDataTemplate x:Key="ProvinceTemplate" ItemsSource="{Binding Citys}" ItemTemplate="{StaticResource CityTemplate}">
 8             <StackPanel>
 9                 <TextBlock Text="{Binding Name}" Foreground="Green"/>
10             </StackPanel>
11         </common:HierarchicalDataTemplate>
12         <common:HierarchicalDataTemplate x:Key="CountryTemplate" ItemsSource="{Binding Privinces}" ItemTemplate="{StaticResource ProvinceTemplate}">
13             <TextBlock Text="{Binding Name}" Foreground="Blue"/>
14         </common:HierarchicalDataTemplate>
15     </UserControl.Resources>
在Resource中设置完HierarchicalDataTemplate,在TreeView控件中调用ItemTemplate就可以了。
<Controls:TreeView x:Name="tvDemo"  ItemTemplate="{StaticResource CountryTemplate}"></Controls:TreeView>
显示结果如下:
值得注意的是,在定义资源文件的时候,设置CityTemplate,ProvinceTemplate和CountryTemplate的顺序不能交换,否则无法查找到相关资源模板,同时,该资源文件也需要放在TreeView控件声明前,否则也是无法找到相关资源模板。
感谢 银光中国网 (SilverlightChina.Net) 提供空间发布代码和演示。
在线演示: http://silverlightchina.net/html/tips/2009/1211/391.html
源码下载: http://silverlightchina.net/uploads/soft/091211/1-091211164055.zip

本文转自

冷秋寒 51CTO博客,原文链接:http://blog.51cto.com/kevinfan/243767 ,如需转载请自行联系原作者

详解Silverlight Treeview的HierarchicalDataTemplate使用相关推荐

  1. 图文详解Silverlight访问MSSQL数据库

    在银光中国网(SilverlightChina.Net)有一篇"Silverlight与常用数据库互操作系列"文章,其中介绍了使用Silverlight存取不同数据库的方法和步骤. ...

  2. 完全详解--Silverlight 下载文件

    1:假设服务器端有文件test.rar:对应的地址是:http://localhost:34270/ClientBin/test.rar: 2:Silverlight要下载这个文件的方式很简单:使用H ...

  3. Silverlight实用窍门系列:54.详解Silverlight中的矩阵变换MatrixTransform,实现其余各种变换【附带实例源码】...

    在Silverlight中的MatrixTransform矩阵变换相对上篇文章所述的变换较复杂一些,但这种变换也更灵活. MatrixTransform的实质:让需要变换的元素上的每一个像素点*矩阵得 ...

  4. [Winodows Phone 7控件详解]Silverlight toolkit for Windows Phone 7.1控件-3

    2.ToggleSwitch 这个开关控件,是windows phone7独有的,Silverlight 没有实现这个控件.这个控件有两个状态Checked 和Unchecked. <Grid ...

  5. [Winodows Phone 7控件详解]Silverlight toolkit for Windows Phone 7.1控件-5

    11年11月初,微软的silverlight toolkit for phone又放出四个新控件,这四个控件使用都要比之前的复杂,并且很有用. 6.AutoCompleteBox 这个控件很像ajax ...

  6. python treeview控件使用详解,python Treeview使用笔记 1

    先贴代码,python2.7 #!/usr/bin/env python # -*- coding:utf-8 -*- import ttk from Tkinter import * class t ...

  7. java treeview使用详解_Javafx Treeview项目操作事件

    我正在尝试使用treeView创建菜单.这是我第一次使用treeView,并且已经在多个网站上进行了阅读. 我在进行动作事件时遇到一些问题.我想要做的基本上是在用户单击树形视图中的某个节点时触发并发生 ...

  8. 稳扎稳打Silverlight(17) - 2.0数据之详解DataGrid, 绑定数据到ListBox

    [索引页] [源码下载] 稳扎稳打Silverlight(17) - 2.0数据之详解DataGrid, 详解ListBox 作者:webabcd 介绍 Silverlight 2.0 详解DataG ...

  9. 稳扎稳打Silverlight(18) - 2.0视频之详解MediaElement, 开发一个简易版的全功能播放器...

    [索引页] [×××] 稳扎稳打Silverlight(18) - 2.0视频之详解MediaElement, 开发一个简易版的全功能播放器 作者:webabcd 介绍 Silverlight 2.0 ...

最新文章

  1. 东财在线计算机第一套作业,东财在线21春《计算机网络基础》第一套作业题目及答案...
  2. 我们眼中的2015年互联网10大产品事件
  3. 【CyberSecurityLearning 4】NTFS安全权限及文件共享服务器
  4. android记账软件开发源代码_如何开发直播软件?直播软件开发的具体流程有哪些?...
  5. 在spring boot中3分钟上手RPC框架Dubbo
  6. runTime动态给类添加属性
  7. 上海java工作经验与薪资_Java硕士京东工作1年,跳槽后他期望薪资26K,大家感觉他可以吗...
  8. 网页上的html表格导出excel表格,网页表格导出至Excel
  9. python参考手册下载_Python中文手册【Word版 】
  10. 常见MFC UI界面库
  11. 前端JS xxxx年xx月xx日转换成页面时间组件xxxx-xx-xx格式
  12. 2019最新《后盾网向军0基础学PHP教程》
  13. python cox模型_Forest plot(森林图) | Cox生存分析可视化
  14. delphi 应用程序开发工具
  15. ENSP中ACL,NAT配置
  16. 沟通的艺术:看人入里,看出人外 - part 1
  17. arduino/mixly红外发射接收模块
  18. 【性能测试】性能测试指标TPS(Transaction per Second)
  19. PCI总线协议(一)
  20. 红外对管应该如何选择比较好

热门文章

  1. OpenCV下车牌定位算法实现代码
  2. java三维滑雪,第六章 三维数据空间分析方法.ppt
  3. php easyui tree 结构,EasyUI Tree树组件无限循环的解决方法
  4. matlab 线模式密度,环形腔窄线宽光纤激光器的研究
  5. java程序ssh置顶_使用shell脚本启动远程(SSH)Java应用程序不会返回本地提示
  6. tomcat端口号被占用怎么解决_电脑C盘空间不够用怎么办?Win7解决C盘占用空间大的3个方法!...
  7. python图像相似度识别_一个用SIFT特征比较图像相似度的python小程序
  8. Java项目:网上图书商城系统(java+SSM+Jsp+MySQL+Redis+JWT+Shiro+RabbitMQ+EasyUI)
  9. Java项目:清新论坛系统(java+SSM+mysql+maven+tomcat)
  10. 【java】兴唐第二十九节课作业