自定义组合框
我有我已经宣布枚举如下命名空间枚举名称:自定义组合框
namespace IXMSoft.Business.SDK.Data
{
using System;
public enum BaudRate
{
BR115200 = 7,
BR19200 = 4,
BR230400 = 8,
BR2400 = 1,
BR38400 = 5,
BR4800 = 2,
BR57600 = 6,
BR9600 = 3
}
}
当我取回在组合框中,这是另一个命名空间这些值,使用语句
comboBox1.Items.Add(BaudRate.BR5700);
它示出了该值作为例如
“BR5700”
我想remove BR in front and just want to display the value as "5700"
。 我该怎么办?
使用DescriptionAttribute
和appropriate extension method来读出它。
public enum BaudRate
{
[Description("115200 kb")]
BR115200 = 7,
[Description("19200 kb")]
BR19200 = 4,
[Description("230400 kb")]
BR230400 = 8,
[Description("2400 kb")]
BR2400 = 1,
[Description("115200 kb")]
BR38400 = 5,
[Description("4800 kb")]
BR4800 = 2,
[Description("57600 kb")]
BR57600 = 6,
[Description("9600 kb")]
BR9600 = 3
}
扩展方法:
public static class EnumExtension
{
/// <summary>
/// Gets the string of an DescriptionAttribute of an Enum.
/// </summary>
/// <param name="value">The Enum value for which the description is needed.</param>
/// <returns>If a DescriptionAttribute is set it return the content of it.
/// Otherwise just the raw name as string.</returns>
public static string Description(this Enum value)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
string description = value.ToString();
FieldInfo fieldInfo = value.GetType().GetField(description);
DescriptionAttribute[] attributes =
(DescriptionAttribute[])
fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
description = attributes[0].Description;
}
return description;
}
/// <summary>
/// Creates an List with all keys and values of a given Enum class
/// </summary>
/// <typeparam name="T">Must be derived from class Enum!</typeparam>
/// <returns>A list of KeyValuePair<Enum, string> with all available
/// names and values of the given Enum.</returns>
public static IList<KeyValuePair<Enum, string>> ToList<T>() where T : struct
{
var type = typeof(T);
if (!type.IsEnum)
{
throw new ArgumentException("T must be an enum");
}
return (IList<KeyValuePair<Enum, string>>)
Enum.GetValues(type)
.OfType<Enum>()
.Select(e => new KeyValuePair<Enum, string>(e, e.Description()))
.ToArray();
}
public static T GetValueFromDescription<T>(string description) where T : struct
{
var type = typeof(T);
if(!type.IsEnum)
{
throw new ArgumentException("T must be an enum");
}
foreach(var field in type.GetFields())
{
var attribute = Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if(attribute != null)
{
if(attribute.Description == description)
{
return (T)field.GetValue(null);
}
}
else
{
if(field.Name == description)
{
return (T)field.GetValue(null);
}
}
}
throw new ArgumentOutOfRangeException("description");
// or return default(T);
}
}
在,你可以简单地通过调用应用此组合框:
var list = EnumExtension.ToList<BaudRate>();
myComboBox.DataSource = list;
myComboBox.ValueMember = "Key";
myComboBox.DisplayMember = "Value";
+1:比其他答案中使用的黑客要好得多,这是特定于此枚举的。 –
@newStackExchangeInstance同意(很喜欢这个答案),但是OP在另一篇文章中提到他无法修改枚举的代码。 –
@newStackExchangeInstance:如果我没有desctiption标签怎么办? 是否仍然可以显示没有BR的值? –
例如用与string.replace:
BaudRate.BR115200.ToString().Replace("BR","");
实例与子:
BaudRate.BR115200.ToString().Substring(2);
删除从枚举名BR似乎是行动的最合理的做法。鉴于你的枚举本身被命名为BaudRate
,BR是多余的。并且,假设它每的值存在于上,它不会为值名称添加任何描述性权力。并且,如果枚举值总是以枚举名称为前缀,则结果将始终清除(BaudRate.9600
而不是BaudRate.BR9600
)。
如果你不能/不想这样做,那么你需要在添加前为每个值运行一个BaudRate.XXX.ToString().Substring(2)
以删除前两个字符。
public enum BaudRate
{
BR115200 = 7,
BR19200 = 4,
BR230400 = 8,
BR2400 = 1,
BR38400 = 5,
BR4800 = 2,
BR57600 = 6,
BR9600 = 3
}
}
foreach (string name in Enum.GetNames(BaudRate))
{
cmbEnum.Items.Add(name.Replace("BR",""));
}
有你的组合框定义一样,如下图所示:
<combobox>
<ComboBoxItem>--</ComboBoxItem>
<ComboBoxItem>2400</ComboBoxItem>
<ComboBoxItem>4800</ComboBoxItem>
<ComboBoxItem>9600</ComboBoxItem>
<ComboBoxItem>19200</ComboBoxItem> // and soo on
</combobox>
a nd绑定你的枚举到组合框
需要使用'替换'mmethod – Rahul
获取字符串,然后将其添加到ComboBox1.String str = BaudRate.BR5700;如果(str.contains(“BR”))str = str.replace(“BR”,“”);希望它可以帮助你。 – MahaSwetha
@MahaSwetha谢谢! :) –