如何将枚举绑定到列表框?
我有一个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();
使用转换器执行此操作。 请参阅https://geekswithblogs.net/cskardon/archive/2008/10/16/databinding-an-enum-in-wpf.aspx 。
将枚举转换为列表(或类似) – 按照如何将枚举转换为C#中的列表?
然后绑定到转换后的列表。
上述就是C#学习教程:如何将枚举绑定到列表框?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/942840.html