我为啥称之为“动态数据模板”?先看看下面的截图,今天,我们就是要实现这种功能。

大概是这样的,我们定义的DataTemplate是通过触发器动态应用到 ComboBoxItem 上。

这个下拉列表控件绑定了一个Person集合,Person类的定义如下:
  1. public class Person
  2. {
  3. public string Name { get; set; }
  4. public int Age { get; set; }
  5. public string Email { get; set; }
  6. public override string ToString()
  7. {
  8. return Name;
  9. }
  10. }
这里重写了ToString方法,因为ComboBox生成的项是调用对象的ToString方法的,为了能不设置数据模板的前提下正确显示列表项,需要重写ToString方法,默认显示姓名属性。
 
然后,我们为ComboBoxItem定义一个处于高亮状态时使用的数据模板,也就是当鼠标移到项上时发生。
  1. <Window.Resources>
  2. <!--
  3. 当项高亮显示时使用的数据模板
  4. -->
  5. <DataTemplate x:Key="hightlightTmp">
  6. <Grid>
  7. <Grid.RowDefinitions>
  8. <RowDefinition Height="auto"/>
  9. <RowDefinition Height="auto"/>
  10. </Grid.RowDefinitions>
  11. <StackPanel Margin="0,5,0,0" Grid.Row="0" Orientation="Horizontal">
  12. <TextBlock Margin="2,0" FontWeight="Bold" FontSize="14">
  13. <TextBlock.Text>
  14. <Binding Path="Name"
  15. StringFormat="姓名:{0}"/>
  16. </TextBlock.Text>
  17. </TextBlock>
  18. <TextBlock Margin="20,0">
  19. <TextBlock.Text>
  20. <Binding Path="Age"
  21. StringFormat="年龄:{0}"/>
  22. </TextBlock.Text>
  23. </TextBlock>
  24. </StackPanel>
  25. <TextBlock Margin="0,2,0,5" Grid.Row="1">
  26. <TextBlock.Text>
  27. <Binding Path="Email"
  28. StringFormat="电邮:{0}"/>
  29. </TextBlock.Text>
  30. </TextBlock>
  31. </Grid>
  32. </DataTemplate>
  33. ..............
  34. </Window.Resources>
为 ComboBoxItem 定义一个样式。
  1. <Window.Resources>
  2. ................
  3. <!-- 项样式 -->
  4. <Style x:Key="cmbStyle" TargetType="{x:Type ComboBoxItem}">
  5. <Style.Triggers>
  6. <Trigger Property="IsHighlighted" Value="True">
  7. <Setter Property="ContentTemplate"
  8. Value="{StaticResource hightlightTmp}"/>
  9. </Trigger>
  10. </Style.Triggers>
  11. </Style>
  12. </Window.Resources>
在窗体中声明一个ComboBox。
  1. <Grid>
  2. <ComboBox x:Name="cmb" Margin="10,10"
  3. Height="24" Width="200"
  4. HorizontalAlignment="Left"
  5. VerticalAlignment="Top"
  6. ItemContainerStyle="{StaticResource cmbStyle}"/>
  7. </Grid>
最后,切换到代码视图,完成设置数据源的C#代码。
  1. public Window1()
  2. {
  3. InitializeComponent();
  4. this.cmb.ItemsSource = new Person[]
  5. {
  6. new Person{Name="小李",Age=22,Email="suk211@163.com"},
  7. new Person{Name="小王",Age=20,Email="minat@126.com"},
  8. new Person{Name="黄涨",Age=21,Email="laned2@21cn.com"},
  9. new Person{Name="高产",Age=22,Email="ha@136.com"},
  10. new Person{Name="杜林",Age=21,Email="null@yaahoo.com"},
  11. new Person{Name="杨白姥",Age=50,Email="cYang@21cn.com"},
  12. new Person{Name="鸟人",Age=31,Email="bgl@ask.net.cn"},
  13. new Person{Name="宋小八",Age=28,Email="xde227@123h.com"}
  14. };
  15. }
完成,这时候运行一下,你会看到上文中截图中的效果了。
 
 

继续聊WPF——动态数据模板的更多相关文章

  1. WPF 动态更换模板

    Window x:Class="模板选择器.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml ...

  2. WPF 寻找数据模板中的元素

    <Window x:Class="Wpf180706.Window11"        xmlns="http://schemas.microsoft.com/wi ...

  3. 转载 WPF -- 控件模板 (ControlTemplate)(一) https://blog.csdn.net/qq_23018459/article/details/79899838

    ControlTemplate(控件模板)   https://blog.csdn.net/qq_23018459/article/details/79899838 WPF包含数据模板和控件模板,其中 ...

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

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

  5. WPF中的数据模板使用方式之一:ContentControl、ContentTemplate和TemplateSelector的使用

    在WPF中,数据模板是非常强大的工具,他是一块定义如何显示绑定的对象的XAML标记.有两种类型的控件支持数据模板:(1)内容控件通过ContentTemplate属性支持数据模板:(2)列表控件通过I ...

  6. WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定

    原文:WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定 WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件) 上面的 ...

  7. WPF 后台获得 数据模板里的内容控件(DataTemplate)

    原文:WPF 后台获得 数据模板里的内容控件(DataTemplate) 假如      <Window.Resources> 里 有一个 Datatemplate 我想获得TextBlo ...

  8. WPF的ComboBox 数据模板自定义

    WPF的ComboBox 有些时候不能满足用户需求,需要对数据内容和样式进行自定义,下面就简要介绍一下用数据模板(DataTemplate)的方式对ComboBox 内容进行定制: 原型设计如下: 步 ...

  9. WPF中的数据模板(DataTemplate)(转)

    原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/30/694388.html WPF中的数据模板(DataTemplate)        ...

随机推荐

  1. Oracle Service Bus Socket Adapter调整的参数

    之前在一个客户中做压力测试时候Oracle Service Bus性能大概达到900tps左右,和客户期望的1600tps有很大差距. 在研究了Socket Adapter的工作原理之后,判断可能是O ...

  2. android开发游记:SpringView 下拉刷新的高效解决方式,定制你自己风格的拖拽页面

    关于下拉刷新/上拉载入很多其它的解决方式网上已经有非常多了,浏览了眼下主流的下拉控件比方PullToRefresh库等.第一:大多数实现库都难以进行动画和样式的自己定义. 第二:不能非常好的兼容多种滚 ...

  3. List<实体>与List<String>数据互转

    1.List<实体>数据: public List<Device> queryOSDevice(String cpu,String ip,String name){ Strin ...

  4. Python连接MySQL乱码(中文变问号)

    #coding=utf-8 import MySQLdb db = MySQLdb.connect("IP","用户名","密码",&quo ...

  5. JMS两种消息模型

    前段时间学习EJB.接触到了JMS(Java消息服务),JMS支持两种消息模型:Point-to-Point(P2P)和Publish/Subscribe(Pub/Sub),即点对点和公布订阅模型. ...

  6. C#开发微信公众平台-就这么简单(转载)

    写在前面 服务号和订阅号 URL配置 创建菜单 查询.删除菜单 接受消息 发送消息(图文.菜单事件响应) 示例Demo下载 后记 最近公司在做微信开发,其实就是接口开发,网上找了很多资料,当然园友也写 ...

  7. 在 XenServer上调试windows程序

    WinDbg WinDbg is one of a number of tools available from Microsoft that can be used for debugging Wi ...

  8. 神奇的canvas——巧用 canvas 为图片添加水印

    代码地址如下:http://www.demodashi.com/demo/11637.html 很久之前写过一篇关于 canvas 的文章,是通过 canvas 来实现一个绚丽的动画效果,不管看过没看 ...

  9. 修改 hostname

    1.修改hostname hostname是一个kernel变量,可以通过hostname命令来查看本机的hostname.也可以直接cat /proc/sys/kernel/hostname查看. ...

  10. angularJS学习笔记(二)

    前言 首先,了解 一下ng的一些概念: module 代码的组织单元,其它东西都是定义在具体的模块中的. app 应用业务,需要多个模块的配合完成. service 仅在数据层面实现特定业务功能的代码 ...