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

大概是这样的,我们定义的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. [Python爬虫] 之二十四:Selenium +phantomjs 利用 pyquery抓取中广互联网数据

    一.介绍 本例子用Selenium +phantomjs爬取中广互联网(http://www.tvoao.com/select.html)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字:融 ...

  2. java.lang.NoClassDefFoundError: com.doodlemobile.gamecenter.Platform

    这时候能够尝试一下下面方法: 右击"项目名"--->"Build path"----->"configure build path&quo ...

  3. window onload 与 img onload事件

    window onload 与 img onload事件 通常,window.onload就是载入完dom之后运行的. 而img就是载入完图片完运行它的onload事件.依据img的src是否载入完毕 ...

  4. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  5. STL坑汇总

    1. Q:vector的push_back()方法到底做了些什么? 为什么声明写的是void push_back (const value_type& val); A:的确,乍一看,似乎pus ...

  6. Visual studio C++ MFC之树形控件Tree Control

    背景 本篇旨在MSDN帮助文档下总结树形控件Tree Control的使用,并列出碰到的具体问题. 正文 树形控件Tree Control的类则是CTreeCtrl,具体成员对象详见链接,以下则描述一 ...

  7. STM32f103按键检测程序实现长按短按

    背景 只要使用单片机,按键检测基本上是一定要实现的功能.按键检测要好用,最重要的是实时和去抖.初学者往往会在主循环调用按键检测程序(实时)并利用延时去抖(准确).这种在主循环内延时的做法对整个程序非常 ...

  8. js中数组的splice()方法

    在数组中splice方法有增.删.该的多功能用处. var list = []; list.push(1); list.push(2); list.push(3); console.log(list) ...

  9. 【Android进阶】怎样使用文件来保存程序中的数据

    在程序中.有非常多保存和获取数据的方法,本篇文章,主要介绍使用文件系统对程序中的数据进行保存和读取的操作 我直接写了一个帮助类,进行文件的写入和读取操作 /** * 用于在文件里保存程序数据 * * ...

  10. 一个请求在Struts2框架中的处理的步骤