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

大概是这样的,我们定义的DataTemplate是通过触发器动态应用到 ComboBoxItem 上。
- public class Person
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public string Email { get; set; }
- public override string ToString()
- {
- return Name;
- }
- }
- <Window.Resources>
- <!--
- 当项高亮显示时使用的数据模板
- -->
- <DataTemplate x:Key="hightlightTmp">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="auto"/>
- <RowDefinition Height="auto"/>
- </Grid.RowDefinitions>
- <StackPanel Margin="0,5,0,0" Grid.Row="0" Orientation="Horizontal">
- <TextBlock Margin="2,0" FontWeight="Bold" FontSize="14">
- <TextBlock.Text>
- <Binding Path="Name"
- StringFormat="姓名:{0}"/>
- </TextBlock.Text>
- </TextBlock>
- <TextBlock Margin="20,0">
- <TextBlock.Text>
- <Binding Path="Age"
- StringFormat="年龄:{0}"/>
- </TextBlock.Text>
- </TextBlock>
- </StackPanel>
- <TextBlock Margin="0,2,0,5" Grid.Row="1">
- <TextBlock.Text>
- <Binding Path="Email"
- StringFormat="电邮:{0}"/>
- </TextBlock.Text>
- </TextBlock>
- </Grid>
- </DataTemplate>
- ..............
- </Window.Resources>
- <Window.Resources>
- ................
- <!-- 项样式 -->
- <Style x:Key="cmbStyle" TargetType="{x:Type ComboBoxItem}">
- <Style.Triggers>
- <Trigger Property="IsHighlighted" Value="True">
- <Setter Property="ContentTemplate"
- Value="{StaticResource hightlightTmp}"/>
- </Trigger>
- </Style.Triggers>
- </Style>
- </Window.Resources>
- <Grid>
- <ComboBox x:Name="cmb" Margin="10,10"
- Height="24" Width="200"
- HorizontalAlignment="Left"
- VerticalAlignment="Top"
- ItemContainerStyle="{StaticResource cmbStyle}"/>
- </Grid>
- public Window1()
- {
- InitializeComponent();
- this.cmb.ItemsSource = new Person[]
- {
- new Person{Name="小李",Age=22,Email="suk211@163.com"},
- new Person{Name="小王",Age=20,Email="minat@126.com"},
- new Person{Name="黄涨",Age=21,Email="laned2@21cn.com"},
- new Person{Name="高产",Age=22,Email="ha@136.com"},
- new Person{Name="杜林",Age=21,Email="null@yaahoo.com"},
- new Person{Name="杨白姥",Age=50,Email="cYang@21cn.com"},
- new Person{Name="鸟人",Age=31,Email="bgl@ask.net.cn"},
- new Person{Name="宋小八",Age=28,Email="xde227@123h.com"}
- };
- }
继续聊WPF——动态数据模板的更多相关文章
- WPF 动态更换模板
Window x:Class="模板选择器.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml ...
- WPF 寻找数据模板中的元素
<Window x:Class="Wpf180706.Window11" xmlns="http://schemas.microsoft.com/wi ...
- 转载 WPF -- 控件模板 (ControlTemplate)(一) https://blog.csdn.net/qq_23018459/article/details/79899838
ControlTemplate(控件模板) https://blog.csdn.net/qq_23018459/article/details/79899838 WPF包含数据模板和控件模板,其中 ...
- [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板
引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate) 切换模板的两种方式: 使用DataTemplateSelecto ...
- WPF中的数据模板使用方式之一:ContentControl、ContentTemplate和TemplateSelector的使用
在WPF中,数据模板是非常强大的工具,他是一块定义如何显示绑定的对象的XAML标记.有两种类型的控件支持数据模板:(1)内容控件通过ContentTemplate属性支持数据模板:(2)列表控件通过I ...
- WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定
原文:WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件)对象绑定 WPF 动态列(DataGridTemplateColumn) 绑定数据 (自定义控件) 上面的 ...
- WPF 后台获得 数据模板里的内容控件(DataTemplate)
原文:WPF 后台获得 数据模板里的内容控件(DataTemplate) 假如 <Window.Resources> 里 有一个 Datatemplate 我想获得TextBlo ...
- WPF的ComboBox 数据模板自定义
WPF的ComboBox 有些时候不能满足用户需求,需要对数据内容和样式进行自定义,下面就简要介绍一下用数据模板(DataTemplate)的方式对ComboBox 内容进行定制: 原型设计如下: 步 ...
- WPF中的数据模板(DataTemplate)(转)
原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/30/694388.html WPF中的数据模板(DataTemplate) ...
随机推荐
- openstack 动态加载usb,需要修改kvm虚拟机的xml文件
一.利用libvirt命令动态挂载 在利用KVM的虚拟桌面应用中,有时候需要在虚拟桌面起来后还能够动态的挂载或卸载数据盘,以达到类似热插盘U盘或移动硬盘的效果,当然管理上需要做处理.如果纯粹中技术上来 ...
- SSH V2的中间人攻击
SSH V2的中间人攻击 2012-12-19 10:48:52 我来说两句 作者:Dis9Team 收藏 我要投稿 中间人攻击(Man-in-the-MiddleAttack ...
- 经典算法——Jump Game(II)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 依据错误原理解决Hibernate执行出现No CurrentSessionContext configured!错误
(1)异常信息例如以下: 严重: Servlet.service() for servlet action threw exception java.lang.RuntimeException: &l ...
- LOGO闪光效果
原地址:http://cl314413.blog.163.com/blog/static/1905079762014122105235138/ 这个效果在很多LOGO及广告宣传中都会用到.商业开发的做 ...
- Openfiler 之Linux 安装ISCSI initiator和自动挂载
OPENFILER做TARGET,RED HAT做客户端,如果默认没有安装ISCSI initiator的话,可以在光盘上找到RPM包直接安装.service iscsi start,启动服务,ser ...
- [C++设计模式] command 命令模式
在软件系统中,"行为请求者"与"行为实现者"通常呈现一种"紧耦合". 但在某些场合,比方要对行为进行"记录.撤销/重做.事务&qu ...
- Android 必知必会 - 依据包名推断 App 执行状态
假设移动端訪问不佳,请訪问: 掘金版 Github 版 获取指定包名的 APP 是否还在后台执行,推断 APP 是否存活. 背景 能够依据 App 是否有 Service 分两类情况处理: 没有 Se ...
- JavaScript | 基础(变量/引用/转换/函数)
———————————————————————————————————————————— 变量 全局变量:在函数体外声明,全局可以使用 局部变量:通过关键字var来声明 变量类型 <script ...
- 可以打开QQ,但打不开网页的DNS服务器设置问题
方法二: IE->设置->连接->局域网设置