如何绑定一个枚举到我的列表框? [英] How do I bind an enum to my listbox?
本文介绍了如何绑定一个枚举到我的列表框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Silverlight(WP7)项目,并愿枚举绑定到一个列表框。这是自定义值枚举,坐在一个类库。我该怎么做呢?
I have a Silverlight (WP7) project and would like to bind an enum to a listbox. This is an enum with custom values, sitting in a class library. How do I do this?
推荐答案
在Silverlight(WP7),Enum.GetNames()方法不可用。您可以使用以下
In Silverlight(WP7), Enum.GetNames() method is not available. You can use the following
public class Enum<T>
{
public static IEnumerable<string> GetNames()
{
var type = typeof(T);
if (!type.IsEnum)
throw new ArgumentException("Type '" + type.Name + "' is not an enum");
return (
from field in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static)
where field.IsLiteral
select field.Name).ToList<string>();
}
}
静态方法将返回枚举字符串集合。您可以将绑定到一个列表框的的ItemsSource。像
The static method will returns enumerable string collection. You can bind that to a listbox's itemssource. Like
this.listBox1.ItemSource = Enum<Colors>.GetNames();
这篇关于如何绑定一个枚举到我的列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文