如何将组合框与Enum绑定 [英] how to bind combobox with Enum
本文介绍了如何将组合框与Enum绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何将组合框与Enum绑定,那么数据源是什么,
显示成员和值会员。?????
我刚尝试使用代码
public Enum MyEnumType
{
[说明(A)]
A = 1,
[说明(B)]
B = 2,
}
comboBox2.DataSource = Enum.GetValues(typeof(MyEnumType));
comboBox1.DisplayMember =Value;
comboBox1.ValueMember =Key;
干杯,
ParvathySunu
添加了代码块 - OriginalGriff [/ edit]
解决方案
你可以找到两个有点长但非常全面的答案我的文章:
枚举类型不会枚举!解决.NET和语言限制 [ ^ ],
人类可读的枚举元数据 [ ^ ]。
-SA
这里有一篇文章解释这个:数据绑定带有描述的枚举 [ ^ ]
最后我得到了解决方案,它正在工作.....
public Enum MyEnumType
{
[Description(A)]
A = 1,
[描述(B)]
B = 2,
}
字典< string,int> dictionary = new Dictionary< string,>();
foreach(int enumValue in
Enum.GetValues(typeof(MyEnumType)) )
{
dictionary.Add(Enum.GetName(typeof(MyEnumType),enumValue),enumValue);
}
ComboBox1.DisplayMember =Key;;
ComboBox1.ValueMember =Value;
ComboBox1.DataSource = new的BindingSource(字典,NULL);
Hi,
I want to knw how to bind combobox with Enum,then what is the datasource,
display member and value member.?????
I just tried with the Code
public Enum MyEnumType { [Description("A")] A= 1, [Description("B")] B= 2, } comboBox2.DataSource = Enum.GetValues(typeof(MyEnumType)); comboBox1.DisplayMember = "Value"; comboBox1.ValueMember = "Key";
Cheers,
ParvathySunu
[edit]Code block added - OriginalGriff[/edit]
解决方案
You can find somewhat long but a very comprehensive answer in two of my articles:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^],
Human-readable Enumeration Meta-data[^].
—SA
There is an article explaining this here: Data Binding an Enum with Descriptions[^]
Finally i have got the solution,Its working.....
public Enum MyEnumType
{
[Description("A")]
A= 1,
[Description("B")]
B= 2,
}
Dictionary<string,int> dictionary=new Dictionary<string,>();
foreach (int enumValue in
Enum.GetValues(typeof(MyEnumType)))
{
dictionary.Add(Enum.GetName(typeof(MyEnumType),enumValue),enumValue);
}
ComboBox1.DisplayMember = "Key";;
ComboBox1.ValueMember = "Value";
ComboBox1.DataSource = new BindingSource(dictionary,null);
这篇关于如何将组合框与Enum绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文