Enum Binding ItemsSource In WPF
Enum Binding ItemsSource In WPF
在WPF中枚举绑定到ItemsSource。
一、通过ObjectDataProvider 获取Enum数据源
首先我们定义一个Enum类:
public enum TableSelectedType
{
SelectedOne,
SelectedTwo,
SelectedThird
}
接着在Xaml中的Resource里定义数据源。
<UserControl.Resources>
<ObjectDataProvider x:Key="odp" MethodName="GetNames" ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:TableSelectedType"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>
我们这里写好了一个Enum数据源,他的key是odp。我们把它绑定到ComboBox的ItemsSource上看下。
<ComboBox ItemsSource="{Binding Source={StaticResource odp}}"/>
效果图:

但是有时候,我们要绑定的是Enum,但想显示它相应的中文字符串。比如“SelectOne”显示为“第一条”。
这里我用到了转换器(Converter).

代码public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string[] enmus = value as string[];
List<string> result=new List<string>();
foreach (var item in enmus)
{
switch (item)
{
case "SelectedOne":
result.Add("第一行");
break;
case "SelectedTwo":
result.Add("第二行");
break;
case "SelectedThird":
result.Add("第三行");
break;
}
}
return result;
}
xaml中:

代码 <ComboBox ItemsSource="{Binding Source={StaticResource odp}, Converter={StaticResource EnumTypeToStringConverter}}"/>
效果:

二、通过Dictionary来存Enum,并绑定到ItemsSource上
这种方便且效率高。我们还是用上面的Enum类型。我们声明一个Dictionary的属性TableSelectedTypeCollection ,并对他初始话。
public partial class ConboBoxEnum : UserControl
{
public ConboBoxEnum()
{
InitializeComponent();
TableSelectedTypeCollection=new Dictionary<TableSelectedType, string>();
TableSelectedTypeCollection.Add(TableSelectedType.SelectedOne,"第一条");
TableSelectedTypeCollection.Add(TableSelectedType.SelectedTwo,"第二条");
TableSelectedTypeCollection.Add(TableSelectedType.SelectedThird,"第三条");
this.DataContext = this;
}
public Dictionary<TableSelectedType, string> TableSelectedTypeCollection { get; set; }
}
Xaml中,我们用TableSelectedTypeCollection 来绑定ComboBox的ItemsSource。
<ComboBox ItemsSource="{Binding TableSelectedTypeCollection}" SelectedValue="{Binding TableSelectedType}" DisplayMemberPath="Value"/>
这里我们用Value来显示它对应的中文字。SelectedValue来绑定它的Enum类型。因为后台我们通常用Enum中的类型。
效果还是一样。 
三、通过特性(Attribute)来获取
这里用到了MS命名空间下的using System.ComponentModel;在枚举元素上加Description这个特性。
public enum TableSelectedType
{
[Description("选择第一行")]
SelectedOne,
[Description("选择第二行")]
SelectedTwo,
[Description("选择第三行")]
SelectedThird
}
我们写一个EnumHelper来获取它。
public static class EnumHelper
{
public static T GetEnumAttribute<T>(Enum source)where T:Attribute
{
Type type = source.GetType();
var sourceName = Enum.GetName(type, source);
FieldInfo field = type.GetField(sourceName);
object[] attributes = field.GetCustomAttributes(typeof (T), false);
foreach (var o in attributes)
{
if (o is T)
return (T) o;
}
return null;
} public static string GetDescription(Enum source)
{
var str = GetEnumAttribute<DescriptionAttribute>(source);
if (str==null)
return null;
return str.Description;
} }
然后我们在实例化这个枚举的时候,调用它就可以。
public Dictionary<TableSelectedType, string> TableSelectedTypeDictionary { get; set; }
public ConboBoxEnum()
{
InitializeComponent();
TableSelectedTypeDictionary=new Dictionary<TableSelectedType, string>();
TableSelectedTypeDictionary.Add(TableSelectedType.SelectedOne, EnumHelper.GetDescription(TableSelectedType.SelectedOne));
TableSelectedTypeDictionary.Add(TableSelectedType.SelectedTwo, EnumHelper.GetDescription(TableSelectedType.SelectedTwo));
TableSelectedTypeDictionary.Add(TableSelectedType.SelectedThird, EnumHelper.GetDescription(TableSelectedType.SelectedThird));
this.DataContext = this;
}
Enum Binding ItemsSource In WPF的更多相关文章
- Listbox Binding ItemsSource
把List<CourseItem>绑定到ListBox. 前台绑定: <ListBox x:Name="ItemBox" Grid.Row="1&quo ...
- Impossible WPF Part 2: Binding Expressions
原文 http://www.11011.net/wpf-binding-expressions Back in April I posted an idea for building an expre ...
- WPF自学入门(七)WPF 初识Binding
今天记录一下Binding的基础和具体的使用方法,说起这个Binding,在WPF中,Binding是很重要的特征,在传统的Windows软件来看,大多数都是UI驱动程序的模式,也可以说事件驱动程序, ...
- WPF Binding ElementName方式无效的解决方法--x:Reference绑定
原文:WPF Binding ElementName方式无效的解决方法--x:Reference绑定 需求: 背景:Grid的有一个TextBlock name:T1和一个ListBox,ListBo ...
- WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...
- WPF仿Word头部格式,涉及DEV RibbonControl,NarvbarControl,ContentPresenter,Navigation
时隔1个月,2015/06/17走进新的环境. 最近一个星期在学习仿Word菜单栏的WPF实现方式,废话不多说,先看一下效果. 打开界面后,默认选中[市场A],A对应的菜单栏,如上图, 选择[市场B] ...
- WPF快速精通版
命名空间: xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:U ...
- WPF DataGrid显格式
Guide to WPF DataGrid formatting using bindings Peter Huber SG, 25 Nov 2013 CPOL 4.83 (13 votes) ...
- 【转】WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 下拉选择控件ComboBox的自定义样式及扩展: 自定义多选控件Mul ...
随机推荐
- [HEOI2014]南园满地堆轻絮
[HEOI2014]南园满地堆轻絮 BZOJ luogu 二分答案贪心check 首先b[1]最小一定优 之后就贪心的最小化b[i]就行 #include<bits/stdc++.h> u ...
- 解决github访问慢和clone慢解决方案
在http://tool.chinaz.com/dns/ 这个网站输入github.com 打开cmd ping各个服务器ip地址,看看哪个比较好 windows下C:\Windows\System3 ...
- MongoDB学习笔记—概念解析
Mongo基本概念 下表将帮助您更容易理解Mongo中的一些概念: SQL术语/概念 MongoDB术语/概念 解释/说明 database database 数据库 table collection ...
- payload有效载荷(转)
payload 记载着信息的那部分数据.通常在传输数据时,为了使数据传输更可靠,要把原始数据分批传输,并且在每一批数据的头和尾都加上一定的辅助信息,比如这一批数据量的大小,校验位等,这样就相当于给已经 ...
- C语言预处理命令的使用
cppreference.com -> 预处理命令 -> 详细说明 预处理命令 #,## # 和 ## 操作符是和#define宏使用的. 使用# 使在#后的首个参数返回为一个带引号的字符 ...
- python3 multiprocessing 模块
多进程 Multiprocessing 模块 multiprocessing 模块官方说明文档 Process 类 Process 类用来描述一个进程对象.创建子进程的时候,只需要传入一个执行函数和函 ...
- Linux sh远程连接失败 sshd.service启动失败
今天不小心在本机的虚拟机执行了 chmod -R 777 /var 导致/var目录下所有权限全部是777 其中 /var/empty/sshd目录权限必须是744,却被改成了777,致使sh远程连接 ...
- C# 利用TTS实现文本转语音
TSS(Text To Speech),语音朗读文本的技术,在Windows下台下,微软给我们提供了一套API接口(Speech API).金山词霸的朗读功能就是用的这个接口. WindowsXP自带 ...
- block(data block,directory block)、inode、块位图、inode位图和super block概念详解【转】
本文转载自:https://blog.csdn.net/jhndiuowehu/article/details/50788287 一.基本概念: 1.block:文件系统中存储数据的最小单元 ...
- centos7 下安装eclipse
1 在下面路径下载 eclipse-jee-neon-2-linux-gtk-x86_64.tar.gzhttp://eclipse.stu.edu.tw/technology/epp/downloa ...