combox绑定枚举和读取枚举

/// 
    /// 绑定对象类
    /// 
    /// 
    public class BindComboxEnumType
    {
        /// 
        /// 类型的名字
        /// 
        public string Name { get; set; }

        /// 
        /// 类型
        /// 
        public T Type { get; set; }

        private static readonly List> bindTyps;

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

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

        /// 
        /// 绑定的类型数据
        /// 
        public static List> BindTyps
        {
            get { return bindTyps; }
        }
    }

//绑定方法            
cbStyle.DataSource = BindComboxEnumType.BindTyps;
            cbStyle.DisplayMember = "Name";
            cbStyle.ValueMember = "Type";

//取选择项对应的枚举值
(Microsoft.Office.Core.MsoPresetTextEffect)cbStyle.SelectedValue

你可能感兴趣的:(c#技巧,c#,combox,combox使用枚举)