wpf-DataTemplate应用
在WPF中,决定数据外观的是DataTemplate,即DataTemplate是数据内容的表现形式,一条数据显示成什么样子,是简单的文本还是直观的图形,就是由DataTemplate决定的。
下面通过设计ListBox及ComboBox控件的DataTemplate,把单调的数据显示成直观的柱状图。
<Window x:Class="WpfDataTemplate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<DataTemplate x:Key="MyItem">
<StackPanel Orientation="Horizontal">
<Grid>
<Rectangle Stroke="Yellow" Fill="Orange" Width="{Binding Price}"></Rectangle>
<TextBlock Text="{Binding Year}"></TextBlock>
</Grid>
<TextBlock Text="{Binding Price}"></TextBlock>
</StackPanel>
</DataTemplate>
</Window.Resources> <StackPanel>
<ListBox ItemTemplate="{StaticResource MyItem}" x:Name="listBox1"></ListBox>
<ComboBox ItemTemplate="{StaticResource MyItem}" x:Name="comboBox1"></ComboBox>
</StackPanel>
</Window>
后台代码:
namespace WpfDataTemplate
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); List<Unit> units = new List<Unit>();
Unit unit1 = new Unit() { Year = "", Price = };
Unit unit2 = new Unit() { Year = "", Price = };
Unit unit3 = new Unit() { Year = "", Price = };
Unit unit4 = new Unit() { Year = "", Price = };
Unit unit5 = new Unit() { Year = "", Price = };
units.Add(unit1);
units.Add(unit2);
units.Add(unit3);
units.Add(unit4);
units.Add(unit5); listBox1.ItemsSource = units;
comboBox1.ItemsSource = units;
}
} public class Unit
{
public string Year { get; set; }
public int Price { get; set; }
}
}
wpf-DataTemplate应用的更多相关文章
- WPF DataTemplate與ControlTemplate
一. 前言 什麼是DataTemplate? 什麼是ControlTemplate? 在stackoverflow有句簡短的解釋 "A DataTemplate, therefore ...
- WPF -- DataTemplate与ControlTemplate结合使用
如深入浅出WPF中的描述,DataTemplate为数据的外衣,ControlTemplate为控件的外衣.ControlTemplate控制控件的样式,DataTemplate控制数据显示的样式,D ...
- silverlight wpf DataTemplate Command binding
<Grid x:Name="LayoutRoot" Background="White"> <CommonControl:NoapDataGr ...
- wpf DataTemplate ColumnDefinition width equal
<Grid.ColumnDefinitions> <ColumnDefinition SharedSizeGroup="col1" /> <Colum ...
- 全局 Style
1.定义一个全局资源文件,如下 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pres ...
- wpf ListView DataTemplate方式的鼠标悬停和选中更改背景色
今天使用wpf技术弄一个ListView的时候,由于需求需要,需要ListView显示不同的数据模板,很自然的使用了DataTemplate方式来定义多个数据模板,并在ListView中使用ItemT ...
- [WPF系列]-数据邦定之DataTemplate 对分层数据的支持
到目前为止,我们仅讨论如何绑定和显示单个集合. 某些时候,您要绑定的集合包含其他集合. HierarchicalDataTemplate 类专用于 HeaderedItemsControl 类型以显示 ...
- [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板
引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate) 切换模板的两种方式: 使用DataTemplateSelecto ...
- [WPF系列]-数据邦定之DataTemplate简介
引言 WPF 数据模板化模型为定义数据的表示形式提供了很大的灵活性.WPF 控件有支持自定义数据表示形式的内置功能.首先介绍下如何定义Datatemplate,然后再介绍其他数据模板化功能,例如根据自 ...
- WPF中UserControl和DataTemplate
最新更新: http://denghejun.github.io 前言 前言总是留给我说一些无关主题的言论,WPF作为全新Microsoft桌面或web应用程序显示技术框架, 从08年开始,一直到现在 ...
随机推荐
- Xcode7.0设置网络白名单
- iOS判断当前控制器是否正在显示
+(BOOL)isCurrentViewControllerVisible:(UIViewController *)viewController { return (viewController.is ...
- Php GMP
GMP是The GNU MP Bignum Library,是一个开源的数学运算库,它可以用于任意精度的数学运算,包括有符号整数.有理数和浮点数.它本身并没有精度限制,只取决于机器的硬件情况. 本函数 ...
- php中双$$与多$$
<?php$a="b";$b="bbb";$c="ccc";echo $$a;?> 输出结果bbb $a的值为b $$a不是输出 ...
- 自定义异常throw
简单自定义一个年龄小于等于0,或者大于120会出现的异常 首先继承父类Exception,调用父类的构造器,这样才可以报出自己想要的异常 public class AgeException exten ...
- jsp页面间的传值
很多的时候我们只是把我们需要的数据,查询出来,然后用request.setAttribute("" ,"" )方法保存这个数据集合.再在我们能跳转到的下一个js ...
- tomcat可以访问到软链接设置
tomcat/conf/context.xml设置<Context allowLinking="true"> 就可以啦.
- 文件夹Tab Ctrl
http://blog.163.com/gz_ricky/blog/static/1820491182011061180897/ 转载 Tab Ctrl Tab属性页控件可以在一个窗口中添加不同的页面 ...
- java反射 -Class类
Class类:任何类都是Class类的对象 Class类的实例对象的三种表现形式:1.通过某个类的.class实现 2.某个类的对象的getClass()方法 3.Class.forName() 注意 ...
- Linux命令记录
端口号 1.查看端口号 使用netstat -anp来查看那些端口被打开.加参数'-n'会将应用程序转为端口显示,即数字格式的地址,如:nfs->2049, ftp->21,因此可以开启两 ...