我有一个枚举,例如:
enum MyEnum
{
My_Value_1,
My_Value_2
}
使用“:
comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));
现在我的问题是:如何将"_"替换为" ",使得项目中有空格而非下划线?并且数据绑定对象仍然能够正常工作。
我有一个枚举,例如:
enum MyEnum
{
My_Value_1,
My_Value_2
}
使用“:
comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));
现在我的问题是:如何将"_"替换为" ",使得项目中有空格而非下划线?并且数据绑定对象仍然能够正常工作。
如果您可以访问 .NET Framework 3.5,那么可以像这样做:
Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.Select(e=> new
{
Value = e,
Text = e.ToString().Replace("_", " ")
});
这将为您返回一个匿名类型的IEnumerable,其中包含一个"Value"属性,该属性是枚举类型本身,并且一个"Text"属性,该属性将包含替换下划线为空格的枚举器的字符串表示形式。/// <summary>
/// Retrieve the description of the enum, e.g.
/// [Description("Bright Pink")]
/// BrightPink = 2,
/// </summary>
/// <param name="value"></param>
/// <returns>The friendly description of the enum.</returns>
public static string GetDescription(this Enum value)
{
Type type = value.GetType();
MemberInfo[] memInfo = type.GetMember(value.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return value.ToString();
}
试一下这个...
comboBox1.DataSource = Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.Select(e => new { Value = e, Description = e.ToString().Replace("_"," ") })
.ToList();
comboBox1.DisplayMember = "Description";
comboBox1.ValueMember = "Value";
尽管如此,我更倾向于使用“描述”属性(正如Steve Crane的回答所述)。
手动填充下拉框并在枚举中进行字符串替换。
以下是具体步骤:
comboBox1.Items.Clear();
MyEnum[] e = (MyEnum[])(Enum.GetValues(typeof(MyEnum)));
for (int i = 0; i < e.Length; i++)
{
comboBox1.Items.Add(e[i].ToString().Replace("_", " "));
}
要设置组合框的已选项,请按以下步骤操作:
comboBox1.SelectedItem = MyEnum.My_Value_2.ToString().Replace("_", " ");
public static class EnumExtensions {
public static List<string> GetFriendlyNames(this Enum enm) {
List<string> result = new List<string>();
result.AddRange(Enum.GetNames(enm.GetType()).Select(s => s.ToFriendlyName()));
return result;
}
public static string GetFriendlyName(this Enum enm) {
return Enum.GetName(enm.GetType(), enm).ToFriendlyName();
}
private static string ToFriendlyName(this string orig) {
return orig.Replace("_", " ");
}
}
然后设置你的组合框,你只需要这样做:
MyEnum val = MyEnum.My_Value_1;
comboBox1.DataSource = val.GetFriendlyNames();
comboBox1.SelectedItem = val.GetFriendlyName();
这应该适用于任何枚举类型。您需要确保在包含EnumExtensions类的命名空间中使用using语句。
我认为将内部枚举名称映射到用户空间并不是一个很好的想法。如果您重构枚举值,会怎么样呢?因此,我建议您查看这篇文章(本地化.NET 枚举)。使用该文章中描述的技术,您不仅可以将“_”替换为空格,还可以为不同的语言制作不同的枚举表示(使用资源)。
我喜欢Kelsey的回答,尽管我会使用另一个变量而不是'e',比如'en',这样答案可以在事件处理程序中更轻松地使用;事件处理程序中的'e'往往是EventArgs
参数。
至于LINQ和IEnumerable
方法,对于非WPF ComboBox
在.NET 3.5中似乎更复杂和难以适应。