如何将枚举绑定到列表框?

我有一个Silverlight(WP7)项目,并希望将枚举绑定到列表框。 这是一个包含自定义值的枚举,位于类库中。 我该怎么做呢?

在Silverlight(WP7)中,Enum.GetNames()方法不可用。 您可以使用以下内容

public class Enum { public static IEnumerable 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(); } } 

静态方法将返回可枚举的字符串集合。 您可以将其绑定到列表框的itemssource。 喜欢

 this.listBox1.ItemSource = Enum.GetNames(); 

使用转换器执行此操作。 请参阅http://geekswithblogs.net/cskardon/archive/2008/10/16/databinding-an-enum-in-wpf.aspx 。

将枚举转换为列表(或类似) – 按照如何将枚举转换为C#中的列表?

然后绑定到转换后的列表。