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

步骤:
1、新建一个WPF应用程序WpfAppDemo(VS2012),并新建一个images文件夹(上传图片素材);
2、在主界面MainWindow.xaml文件中添加一个Label、ComboBox 和Button控件,如下图:

代码如下:

 <Window x:Class="WpfAppDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox x:Name="myColorComBox" HorizontalAlignment="Left" Margin="110,79,0,0" VerticalAlignment="Top" Width="309" Height="48" >
<!--ItemTemplate-->
<ComboBox.ItemTemplate>
<!--DataTemplate开始-->
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="36"/>
<ColumnDefinition Width="100*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50*"/>
<RowDefinition Height="50*"/>
</Grid.RowDefinitions>
<!--绑定数据对象Image属性-->
<Image Source="{Binding Image}" Width="32" Height="32" Margin="3,3,3,3" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" />
<!--绑定数据对象Name属性-->
<TextBlock Text="{Binding Name}" FontSize="12" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<!--绑定数据对象Desc属性-->
<TextBlock Text="{Binding Desc}" FontSize="10" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
<!--DataTemplate结束-->
</ComboBox.ItemTemplate>
</ComboBox>
<Label Content="员工: " HorizontalAlignment="Left" Margin="66,92,0,0" VerticalAlignment="Top"/>
<Button Content="显示" HorizontalAlignment="Left" Margin="110,166,0,0" VerticalAlignment="Top" Width="75" Height="22" Click="Button_Click"/> </Grid> </Window>

3、在主界面MainWindow.xaml.cs文件中进行逻辑处理,代码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WpfAppDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//初始化数据,并绑定
LodData();
} private void LodData()
{
IList<empoyee> customList = new List<empoyee>();
//项目文件中新建一个images文件夹,并上传了001.png,002.png,003.png
customList.Add(new empoyee() { ID ="", Name = "Jack" ,Image="images/001.png",Desc="this is good gay!"});
customList.Add(new empoyee() { ID = "", Name = "Smith", Image = "images/002.png", Desc = "this is great gay!" });
customList.Add(new empoyee() { ID = "", Name = "Json", Image = "images/003.png", Desc = "this is the bset gay!" }); this.myColorComBox.ItemsSource = customList;//数据源绑定
this.myColorComBox.SelectedValue = customList[];//默认选择项 } private void Button_Click(object sender, RoutedEventArgs e)
{
//显示选择的员工ID信息
MessageBox.Show((this.myColorComBox.SelectedItem as empoyee).ID);
} }
//定义员工类
public class empoyee
{
public string ID { get; set; }
public string Name { get; set; }
public string Image { get; set; }
public string Desc { get; set; } }
}

4、编译运行,如果运气不错的话,应该能看到如下的界面:

WPF的ComboBox 数据模板自定义的更多相关文章

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

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

  2. WPF中的数据模板(DataTemplate)

    原文:WPF中的数据模板(DataTemplate) WPF中的数据模板(DataTemplate)                                                   ...

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

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

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

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

  5. wpf中的数据模板

    wpf中的模板分为数据模板和控件模板,我们可以通过我们自己定制的数据模板来制定自己想要的数据表现形式.例如:时间的显示可以通过图片,也可以通过简单数字表现出来. 例如: (1)先在Demo这个命名空间 ...

  6. ItemsControl绑定的数据模板显示不同样式:模板选择器

    总所周知,wpf提供了数据模板,列表控件可以绑定数据实现批量显示同类型数据.不过同个数据模板显示不同的样式怎么办?这时我们可以用模板选择器. 首先我们可以将数据绑定到首先定义资源样式 <Data ...

  7. WPF 自定义ComboBox样式,自定义多选控件

    原文:WPF 自定义ComboBox样式,自定义多选控件 一.ComboBox基本样式 ComboBox有两种状态,可编辑和不可编辑状态.通过设置IsEditable属性可以切换控件状态. 先看基本样 ...

  8. WPF数据模板(7)

    数据模板常用在3种类型的控件, 下图形式: 1.Grid这种列表表格中修改Cell的数据格式, CellTemplate可以修改单元格的展示数据的方式. 2.针对列表类型的控件, 例如树形控件,下拉列 ...

  9. 《Programming WPF》翻译 第5章 5.数据模板和样式

    原文:<Programming WPF>翻译 第5章 5.数据模板和样式 让我们想象一下我们想要实现TTT更有娱乐性的一个版本(这是大部分游戏中最重要的特色).例如,TTT的一种变体允许玩 ...

随机推荐

  1. JS中的匿名函数

    整理自:http://www.cnblogs.com/playerlife/archive/2012/10/17/2727683.html 一.什么是匿名函数? 在Javascript定义一个函数一般 ...

  2. maven项目部署打包

    方法一.把maven依赖的jar包一起打包 http://maven.apache.org/plugins/maven-assembly-plugin/usage.html pom/build中加入以 ...

  3. EntityFramework系列:MySql的RowVersion

    无需修改实体和配置,在MySql中使用和SqlServer一致的并发控制.修改RowVersion类型不可取,修改为Timestamp更不可行.Sql Server的RowVersion生成一串唯一的 ...

  4. spring 源码分析之BeanPostProcessor

    1.官方解答: Factory hook that allows for custom modification of new bean instances, e.g. checking for ma ...

  5. Testing - FURPS模型

    FURPS wiki - FURPS FURPS是功能.易用性.可靠度.性能及可支持性(supportability)五个词英文前缀的缩写,是一种识别软件质量属性的模型. 其中功能部份对应功能需求,另 ...

  6. Mina架构与优化指南

    MINA架构 这里,我借用了一张Trustin Lee在Asia 2006的ppt里面的图片来介绍MINA的架构. Remote Peer就是客户端,而下方的框是MINA的主要结构,各个框之间的箭头代 ...

  7. Azure PowerShell (1) PowerShell整理

    <Windows Azure Platform 系列文章目录> 把之前Azure ASM的PowerShell都整理好了. https://github.com/leizhang1984/ ...

  8. 配置springmvc在其他类中(spring容器外)获取注入bean

    学习https://github.com/thinkgem/jeesite 今天在写JedisUtils的时候要注入JedisPool,而这个属性被设置为static,@Resource和@Autow ...

  9. 四、BLE(中)

    1.1       GATT Manager GATT MGR模块管理所有的GATT服务,同时也是连接GATT模块与GATT ServiceS模块的桥梁. 1.1.1    主要功能模块 先来看一张该 ...

  10. [转]virtualenv建立多个Python独立开发环境

    不同的人喜欢用不同的方式建立各自的开发环境,但在几乎所有的编程社区,总有一个(或一个以上)开发环境让人更容易接受. 使用不同的开发环境虽然没有什么错误,但有些环境设置更容易进行便利的测试,并做一些重复 ...