silverlight中使用enum给combobox添加数据,enum的description属性

Enum: Binding to the Description Attribute

这篇文章写得是C#环境下,带有description属性的enum如何binding到combobox。并且用到了扩展类,使用很方便。

但是silverlight中没有GetValues这个方法,所以不能用。在silverlight中可以用下面的方法

myCombobox.AddComboboxItem(((Customer.CustomerTypeOption)i).GetDescription(), ((Customer.CustomerTypeOption)i).ToString());



public static void AddComboboxItem(this RadComboBox cbo, string description, string value)
{
cbo.Items.Add(new RadComboBoxItem() { Content = description, Tag = value });
}

GetDescription方法:

public static string GetDescription(this Enum currentEnum)
{
string description = String.Empty;
DescriptionAttribute da;
FieldInfo fi = currentEnum.GetType(). GetField(currentEnum.ToString());
da = (DescriptionAttribute)Attribute.GetCustomAttribute(fi,typeof(DescriptionAttribute));
if (da != null)
description = da.Description;
else
description = currentEnum.ToString();
return description;
}

 

posted @ 2012-02-29 13:13  左手边的爱  阅读(545)  评论(0编辑  收藏  举报