在WPF中,决定数据外观的是DataTemplate,即DataTemplate是数据内容的表现形式,一条数据显示成什么样子,是简单的文本还是直观的图形,就是由DataTemplate决定的。
下面通过设计ListBox及ComboBox控件的DataTemplate,把单调的数据显示成直观的柱状图。
<Window x:Class="DataTemplateDemo.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="153" Width="300">
    <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>
后台代码:
public partial class Window2 : Window
{
   
    public Window2()
    {
        InitializeComponent();
        List<Unit> units = new List<Unit>();
        Unit unit1 = new Unit() { Year = "2001", Price=100 };
        Unit unit2 = new Unit() { Year = "2002", Price = 120 };
        Unit unit3 = new Unit() { Year = "2003", Price = 140 };
        Unit unit4 = new Unit() { Year = "2004", Price = 160 };
        Unit unit5 = new Unit() { Year = "2005", Price = 180 };
        units.Add(unit1);
        units.Add(unit2);
        units.Add(unit3);
        units.Add(unit4);
        units.Add(unit5);
        listBox1.ItemsSource = units;
        comboBox1.ItemsSource = units;
    }
}
class Unit
{
    public string Year { get; set; }
    public int Price { get; set; }
}

效果如下图:

也可以把DataTemplate作用在某个数据类型上,方法是设置DataTemplate的DataType属性。上面的例子也可以通过这种方式实现:
<Window x:Class="DataTemplateDemo.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataTemplateDemo"
        xmlns:c="clr-namespace:System.Collections;assembly=mscorlib"
        Title="Window3" Height="153" Width="300">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:MyUnit}">
            <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>
        <c:ArrayList x:Key="ds">
            <local:MyUnit Year = "2001" Price="100"/>
            <local:MyUnit Year = "2002" Price="120"/>
            <local:MyUnit Year = "2003" Price="140"/>
            <local:MyUnit Year = "2004" Price="160"/>
            <local:MyUnit Year = "2005" Price="180"/>
        </c:ArrayList>
    </Window.Resources>
    <StackPanel>
        <ListBox x:Name="listBox1" ItemsSource="{StaticResource ds}"></ListBox>
        <ComboBox x:Name="comboBox1" ItemsSource="{StaticResource ds}"></ComboBox>
    </StackPanel>
</Window>
后台代码:
public class MyUnit
{
    public string Year { get; set; }
    public int Price { get; set; }
}

备注:本例来源于刘铁锰先生的《深入浅出WPF》。

DataTemplate应用的更多相关文章

  1. wpf ListView DataTemplate方式的鼠标悬停和选中更改背景色

    今天使用wpf技术弄一个ListView的时候,由于需求需要,需要ListView显示不同的数据模板,很自然的使用了DataTemplate方式来定义多个数据模板,并在ListView中使用ItemT ...

  2. [WPF系列]-数据邦定之DataTemplate 对 ItemsControl 进行样式和模板处理

    引言   即使 ItemsControl 不是 DataTemplate 所用于的唯一控件类型,将 ItemsControl 绑定到集合仍然很常见. 在 DataTemplate 中有哪些内容一节中, ...

  3. [WPF系列]-数据邦定之DataTemplate 对分层数据的支持

    到目前为止,我们仅讨论如何绑定和显示单个集合. 某些时候,您要绑定的集合包含其他集合. HierarchicalDataTemplate 类专用于 HeaderedItemsControl 类型以显示 ...

  4. [WPF系列]-数据邦定之DataTemplate 使用 DataTrigger 来应用属性值

    使用 DataTrigger 来应用属性值 当前表示不会告诉我们某个 Task 是家庭任务还是办公室任务.记住 Task 对象拥有类型为 TaskType 的 TaskType 属性,该类型是一个枚举 ...

  5. [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板

      引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate)   切换模板的两种方式:   使用DataTemplateSelecto ...

  6. [WPF系列]-数据邦定之DataTemplate简介

    引言 WPF 数据模板化模型为定义数据的表示形式提供了很大的灵活性.WPF 控件有支持自定义数据表示形式的内置功能.首先介绍下如何定义Datatemplate,然后再介绍其他数据模板化功能,例如根据自 ...

  7. WPF中UserControl和DataTemplate

    最新更新: http://denghejun.github.io 前言 前言总是留给我说一些无关主题的言论,WPF作为全新Microsoft桌面或web应用程序显示技术框架, 从08年开始,一直到现在 ...

  8. toolkit:Accordion DataTemplate ListBox TextBlock Interaction.Triggers

    困扰好几个小时的问题终于解决了,本人系菜鸟,使用MVVM设计模式,绑定DataTemplate的Command,需要使用 DataContent的资源,否则无法触发ICommand ClickChil ...

  9. WPF中ControlTemplate和DataTemplate的区别

    下面代码很好的解释了它们之间的区别: <Window x:Class="WPFTestMe.Window12" xmlns="http://schemas.micr ...

随机推荐

  1. C# 协变out 、逆变 in

    需求:泛型使用多态性 备注:协变逆变只能修饰 接口和委托 简单理解: 1.使用 in 修饰后为逆变,只能用作形参使用 ,参考 public delegate void Action<in T&g ...

  2. Quickling技术

    技术来自facebook,目的是实现ajax的seo友好. 简单来说就是骗爬虫,然后该写js还是写js. 原文在这里:http://www.cnblogs.com/haoming/p/3616326. ...

  3. kafka C客户端librdkafka producer源码分析

    from:http://www.cnblogs.com/xhcqwl/p/3905412.html kafka C客户端librdkafka producer源码分析 简介 kafka网站上提供了C语 ...

  4. mouse scrollings and zooming operations in linux & windows are opposite

    mouse scrollings and zooming operations in linux & windows are opposite. windows中, 鼠标滚动的方向是: 查看页 ...

  5. HTML5+CSS3+jquery实现简单的音乐播放器

    ...最近天热的,感觉就像煎饼...然后别人在把妹子的时候,只有偶们这帮苦逼的程序员在那边撸代码...我日哦! 然后今天晒的是偶早年写的一个播放器...看上去是不是很有感觉的样子!一番宝物,Lisa唱 ...

  6. 【AngularJS】—— 13 服务Service

    在AngularJS中有很多的服务,常用的比如$http,$location等等. 本篇文章会介绍一下的内容: 1 $http这种Angular提供的服务的使用 2 如何自定义服务,并总结服务需要注意 ...

  7. R入门-第一次写了一个完整的时间序列分析代码

    纪念一下,在心心念念想从会计本科转为数据分析师快两年后,近期终于迈出了使用R的第一步,在参考他人的例子前提下,成功写了几行代码.用成本的角度来说,省去了部门去买昂贵的数据分析软件的金钱和时间,而对自己 ...

  8. HDOJ 4751 Divide Groups

    染色判断二分图+补图 比赛的时候题意居然是反的,看了半天样例都看不懂 .... Divide Groups Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  9. loadrunner 学习笔记--AJAX(转)

    用loadrunner测试WEB程序的时候总是会碰到AJAX或者ActiveX实现的功能,而通常这些功能会包含很多客户端函数(一般为JavaScript).我们该如何处理?如果从功能实现的角度去考虑这 ...

  10. PPPoE名词解释

    PPPoE拔号的发现阶段(Discovery): PPPoE的发现阶段一共分为4步. 分别是: PADI(PPPoE Active Discovery Initiation) PADO(PPPoE A ...