WPF中的DataTemplate
<Window x:Class="DateTemplate应用.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DateTemplate应用"
xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!--DataTemplate-->
<DataTemplate DataType="{x:Type local:Unit}">
<Grid>
<StackPanel Orientation="Horizontal">
<Grid>
<Rectangle Stroke="Yellow" Fill="Orange" Width="{Binding Price}"/>
<TextBox Text="{Binding Year}"/>
</Grid>
<TextBlock Text="{Binding Price}" Margin="5,0"/>
</StackPanel>
</Grid>
</DataTemplate>
<!--数据源-->
<collections:ArrayList x:Key="ds">
<local:Unit Year="2001" Price="100"/>
<local:Unit Year="2002" Price="120"/>
<local:Unit Year="2003" Price="140"/>
<local:Unit Year="2004" Price="160"/>
<local:Unit Year="2006" Price="180"/>
<local:Unit Year="2008" Price="200"/>
</collections:ArrayList>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{StaticResource ds}"/>
<ComboBox ItemsSource="{StaticResource ds}"/>
</StackPanel>
</Window>

以XML数据格式来显示
<Window x:Class="XMLDataTemplate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:xmlDataTemplate="clr-namespace:XMLDataTemplate"
Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen">
<Window.Resources>
<!--DataTemplate-->
<DataTemplate x:Key="MyXmlDataTemplate">
<Grid>
<StackPanel Orientation="Horizontal">
<Grid>
<Rectangle Stroke="Yellow" Fill="Orange" Width="{Binding XPath=@Price}"/>
<TextBlock Text="{Binding XPath=@Year}"/>
</Grid>
<TextBlock Text="{Binding XPath=@Price}" Margin="5,0"/>
</StackPanel>
</Grid>
</DataTemplate>
<!--数据源-->
<XmlDataProvider x:Key="Ds" XPath="Units/Unit">
<x:XData>
<Units xmlns="">
<Unit Year="2001" Price="100"/>
<Unit Year="2002" Price="120"/>
<Unit Year="2001" Price="140"/>
<Unit Year="2001" Price="160"/>
<Unit Year="2001" Price="180"/>
<Unit Year="2001" Price="200"/>
<Unit Year="2001" Price="220"/>
</Units>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<StackPanel>
<ListBox ItemsSource="{Binding Source={StaticResource Ds}}" ItemTemplate="{StaticResource MyXmlDataTemplate}"/>
<ComboBox ItemsSource="{Binding Source={StaticResource Ds}}" ItemTemplate="{StaticResource MyXmlDataTemplate}"/>
</StackPanel>
</Window>
显示层级数据的模板是HierarchicaDataTemplate
<?xml version="1.0" encoding="utf-8" ?>
<Data xmlns="">
<Grade Name="一年级">
<Class Name="1班">
<Group Name="A组"/>
<Group Name="B组"/>
<Group Name="C组"/>
<Group Name="D组"/>
</Class>
</Grade>
<Grade Name="二年级">
<Class Name="1班">
<Group Name="A组"/>
<Group Name="B组"/>
<Group Name="C组"/>
<Group Name="D组"/>
</Class>
</Grade>
<Grade Name="三年级">
<Class Name="1班">
<Group Name="A组"/>
<Group Name="B组"/>
<Group Name="C组"/>
<Group Name="D组"/>
</Class>
</Grade>
</Data>
<Window x:Class="TreeView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!--数据源-->
<XmlDataProvider x:Key="ds" Source="Data.xml" XPath="Data/Grade"/>
<!--年级模板-->
<HierarchicalDataTemplate DataType="Grade" ItemsSource="{Binding XPath=Class}">
<TextBlock Text="{Binding XPath=@Name}"></TextBlock>
</HierarchicalDataTemplate>
<!--班级模板-->
<HierarchicalDataTemplate DataType="Class" ItemsSource="{Binding XPath=Group}">
<RadioButton Content="{Binding XPath=@Name}" GroupName="gn"></RadioButton>
</HierarchicalDataTemplate>
<!--小组模板-->
<HierarchicalDataTemplate DataType="Group" ItemsSource="{Binding XPath=Student}">
<CheckBox Content="{Binding XPath=@Name}"></CheckBox>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView ItemsSource="{Binding Source={StaticResource ds}}"></TreeView>
</Grid>
</Window>

WPF中的DataTemplate的更多相关文章
- WPF 中的DataTemplate 的嵌套
<Window x:Class="WPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xa ...
- wpf 中的DataTemplate 绑定控件
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...
- WPF中UserControl和DataTemplate
最新更新: http://denghejun.github.io 前言 前言总是留给我说一些无关主题的言论,WPF作为全新Microsoft桌面或web应用程序显示技术框架, 从08年开始,一直到现在 ...
- WPF中的数据模板(DataTemplate)(转)
原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/30/694388.html WPF中的数据模板(DataTemplate) ...
- WPF中的数据模板(DataTemplate)
原文:WPF中的数据模板(DataTemplate) WPF中的数据模板(DataTemplate) ...
- WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探
原文:WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探 最近因为项目需要,开始学习如何使用WPF开发桌面程序.使用WPF一段时间之后,感 ...
- wpf中在style的template寻找ControlTemplate和DataTemplate的控件
一.WPF中的两棵树 WPF中每个控件的Template都是由ControlTemplate构成,ControlTemplate包含了构成该控件的各种子控件,这些子控件就构成了VisualTree:而 ...
- WPF入门教程系列十八——WPF中的数据绑定(四)
六.排序 如果想以特定的方式对数据进行排序,可以绑定到 CollectionViewSource,而不是直接绑定到 ObjectDataProvider.CollectionViewSource 则会 ...
- WPF中的数据绑定!!!
引用自:https://msdn.microsoft.com/zh-cn/magazine/cc163299.aspx 数据点: WPF 中的数据绑定 数据点 WPF 中的数据绑定 John Pap ...
随机推荐
- netbeans 调试 php
修改php.ini文件 原来配置 [XDebug];zend_extension = "E:\xampp\php\ext\php_xdebug.dll";xdebug.profil ...
- apache2.4的安装与卸载
安装sudo apt-get install apache2,这不是源码安装的方式,产生的apache地址在/etc/apache2,配置文件是apache2.conf如果浏览器输入127.0.0.1 ...
- linux-启动停止重启shell 简单shell示例
停止: #!/bin/bashpid=`ps -ef|grep /opt/lampp|grep -v grep|awk '{print $2}'|wc -l`b=0if [ $pid -gt $b ] ...
- Sass和Compass设计师指南
注:配合查看:Sass 为什么使用Sass和Compass? 1.减少重复工作,加快开发进度 2.使用变量,便于记忆,变量使用$符号开头 3.自动转换RGBA颜色值 .color { color: $ ...
- 提高HTML5 Canvas性能的技巧
详细内容请点击 一:使用缓存技术实现预绘制,减少重复绘制Canvs内容 很多时候我们在Canvas上绘制与更新,总是会保留一些不变的内容,对于这些内容 应该预先绘制缓存,而不是每次刷新. 直接绘制代码 ...
- jQuery中的DOM操作<思维导图>
DOM是Document Object Model的缩写,意思是文档对象模型.DOM是一种与浏览器.平台.语言无关的接口.使用该接口可以轻松地访问页面中所有的标准组件.简单来说,DOM解决了Netsc ...
- 遇到的 autoresizingMask 相关的问题
1.前言 当一个控件设置好 frame,然后出现会 frame 显示不准或是跟随父控件的变化而变化了,你就要考虑是否是 autoresizing 的问题了 当在 xib 中布局时,报 NSAutore ...
- JQuery自定义属性的设置和获取
Jquery操作自定义属性的方法,很简洁: $("#test").attr("test","aaa") // 设置 $("#tes ...
- 杭电ACM2058--The sum problem
http://acm.hdu.edu.cn/showproblem.php?pid=2058 以为简单的穷举就完了,结果是一直Time Limit Exceeded.. 这是代码: #include ...
- uml的关联多重度
UML中关联的多重度是指一个类的实例能够与另一个类的多少个实例相关联,这个“多少”被称为关联角色的多重度指定关联一端的多重度.也可以这样理解:在关联另一端的类的每个对象要求在本端的类必须有多 少个对象 ...