.NET数据绑定组合框的字符串枚举与描述属性 [英] .NET databinding a combobox to a string enum with Description attributes
问题描述
我有一个这样的枚举:
public enum Cities
{
[Description("New York City")]
NewYork,
[Description("Los Angeles")]
LosAngeles,
Washington,
[Description("San Antonio")]
SanAntonio,
Chicago
}
我想结合这一个组合框,我已经尝试过这样的:
I want to bind this to a combobox and I've tried this:
comboBox.DataSource = Enum.GetNames(typeof(Cities));
但是,这显示在组合框中,而不是字符串描述的值。所以,我切换到这一点:
But that displays the values in the combobox rather than the String description. So I switched to this:
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
return attributes[0].Description;
}
else
{
return value.ToString();
}
}
public static IList ToList(this Type type)
{
ArrayList list = new ArrayList();
Array enumValues = Enum.GetValues(type);
foreach (Enum value in enumValues)
{
list.Add(new KeyValuePair<Enum, string>(value, GetEnumDescription(value)));
}
return list;
}
现在的价值list.Add()调用的结果,它的显示在组合框中字符串描述让我换成
Now the list.Add() call results in the value and it's string description being displayed in the combobox so I replaced
list.Add(new KeyValuePair<Enum, string>(value, GetEnumDescription(value)));
与
list.Add(GetEnumDescription(value));
,现在我想起来的这是我最终想要的下拉列表中显示的描述字符串。现在我的数据绑定被打破,因为它无法找到刚才枚举中的字符串描述。我想这可能与combobox.DisplayMember和combobox.ValueMember,但我一直没能解决这个问题呢。谁能告诉我如何赫克我显示的描述字符串,但有我的数据绑定使用值存储,等等?谢谢!
and now I'm getting just the descriptive string displayed in the combobox which is what I ultimately want. Now my data binding is broken because it can't find just the string description in the enumeration. I thought this might be related to combobox.DisplayMember and combobox.ValueMember but I haven't been able to resolve the problem yet. Can anyone tell me how the heck I display the descriptive string but have my data binding use the value for storing, etc.? Thank you!!!
推荐答案
让我们回到你的问题我answered前几天和修改,以满足您新的要求。所以,我会守在原地的城市
枚举在这个问题的 colorEnum
的例子。
Let's go back to your question I answered a few days ago and modify that to suit your new requirements. So I'll keep the colorEnum
example in place of your Cities
enum in this question.
您是大部分的方式出现 - 你已经得到了code从枚举描述字符串去;现在你只需要回去的其他方式。
You're most of the way there - you've got the code to go from the enum to the description string; now you just need to go back the other way.
public static class EnumHelper
{
// your enum->string method (I just decluttered it a bit :))
public static string GetEnumDescription(Enum value)
{
var fi = value.GetType().GetField(value.ToString());
var attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
return ((DescriptionAttribute)attributes[0]).Description;
else
return value.ToString();
}
// the method to go from string->enum
public static T GetEnumFromDescription<T>(string stringValue)
where T : struct
{
foreach (object e in Enum.GetValues(typeof(T)))
if (GetEnumDescription((Enum)e).Equals(stringValue))
return (T)e;
throw new ArgumentException("No matching enum value found.");
}
// and a method to get a list of string values - no KeyValuePair needed
public static IEnumerable<string> GetEnumDescriptions(Type enumType)
{
var strings = new Collection<string>();
foreach (Enum e in Enum.GetValues(enumType))
strings.Add(GetEnumDescription(e));
return strings;
}
}
现在,把你有什么前几天...
Now, take what you had a few days ago...
public class Person
{
[...]
public colorEnum FavoriteColor { get; set; }
public string FavoriteColorString
{
get { return FavoriteColor.ToString(); }
set { FavoriteColor = (colorEnum)Enum.Parse(typeof(colorEnum), value); }
}
}
和刚刚改成这样:
public class Person
{
[...]
public colorEnum FavoriteColor { get; set; }
public string FavoriteColorString
{
get { return EnumHelper.GetEnumDescription(FavoriteColor); }
set { FavoriteColor = EnumHelper.GetEnumFromDescription<colorEnum>(value); }
}
}
和以前一样,你会绑定组合框的SelectedItem
值 FavoriteColorString
。你并不需要,如果你还在使用的BindingSource因为你在其他的问题,我认为你是设置的DisplayMember和ValueMember属性。
As before, you'll bind the combobox SelectedItem
value to FavoriteColorString
. You don't need to set the DisplayMember or ValueMember properties if you're still using the BindingSource as you were in the other question, which I assume you are.
和改变组合框的填充code:
And change the combobox populating code from:
comboBoxFavoriteColor.DataSource = Enum.GetNames(typeof(colorEnum));
到
comboBoxFavoriteColor.DataSource = EnumHelper.GetEnumDescriptions(typeof(colorEnum));
现在您拥有了世界上最好的。用户看到的描述,你的code包含枚举名称,数据存储包含枚举值。
Now you have the best of all worlds. The user sees the description, your code contains the enum names, and the data store contains the enum values.
这篇关于.NET数据绑定组合框的字符串枚举与描述属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!