winform下 的combox绑定枚举的通用类

    技术2022-05-19  21

    原始帖子

    http://www.cnblogs.com/yahle/archive/2010/08/25/1807946.html

    这个帖子里实现了通用类,但是实现方法里加载的都是枚举的名字,

    http://wenwen.soso.com/z/q168310960.htm

    这个帖子实现了combox里选择值是数字,显示是名字,但是没有通用类。

     

    呵呵,我就是总结了下,

     

     public class BindComboxEnumType<T>    {        public string Name        {            get;            set;        }

            public string  TypeValue        {            get;            set;        }

            private static readonly List<BindComboxEnumType<T>> bindTypes;

            static BindComboxEnumType()        {            bindTypes = new List<BindComboxEnumType<T>>();

                Type t = typeof(T);

                foreach (FieldInfo  field in t.GetFields())            {                if (field.IsSpecialName == false)                {                    BindComboxEnumType<T> bind = new BindComboxEnumType<T>();                    bind.Name = field.Name;                    bind.TypeValue = field.GetRawConstantValue().ToString();                    bindTypes.Add(bind);                }                          }        }

            public static  List<BindComboxEnumType<T>> BindTypes        {            get {              return  bindTypes;            }        }    }

     

    调用

    ComboBox cb = new ComboBox();

     cb.DataSource = BindComboxEnumType<MethodTypeEnum>.BindTypes;

      cb.DisplayMember = "Name";

      cb.ValueMember = "TypeValue";

       return cb;


    最新回复(0)