天天看点

combox绑定枚举和读取枚举

/// <summary>
    /// 绑定对象类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BindComboxEnumType<T>
    {
        /// <summary>
        /// 类型的名字
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 类型
        /// </summary>
        public T Type { get; set; }

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

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

            foreach (var name in Enum.GetNames(typeof(T)))
            {
                bindTyps.Add(new BindComboxEnumType<T>()
                {
                    Name = name,
                    Type = (T)Enum.Parse(typeof(T), name)
                });
            }
        }

        /// <summary>
        /// 绑定的类型数据
        /// </summary>
        public static List<BindComboxEnumType<T>> BindTyps
        {
            get { return bindTyps; }
        }
    }
           
//绑定方法            
cbStyle.DataSource = BindComboxEnumType<Microsoft.Office.Core.MsoPresetTextEffect>.BindTyps;
            cbStyle.DisplayMember = "Name";
            cbStyle.ValueMember = "Type";
           
//取选择项对应的枚举值
(Microsoft.Office.Core.MsoPresetTextEffect)cbStyle.SelectedValue