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).

代码
xaml中:

代码
效果:
二、通过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; }
作者:dingli
出处:http://www.cnblogs.com/dingli/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界