将枚举绑定的组合框选定值设置为枚举值

时间:2012-08-23 17:13:49

标签: c# winforms binding combobox enums

我的项目中有一个这样的枚举:

public enum UserFrienlyEnum
{
    [Description("it need spec training")]
    SPECIAL_TRAINING = 1,
    [Description("it need normal training")]
    NORMAL_TRAINING = 2,
    [Description("it need simple training")]
    SIMPLE_TRAINING = 3
}

我使用这种方法将这个枚举绑定到一个组合框:

public static void setEnumValues(ComboBox cxbx, Type typ)
{
    if (!typ.IsEnum)
    {
        throw new ArgumentException("Only Enum types can be set");
    }

    List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();

    foreach (int i in Enum.GetValues(typ))
    {
        string name = Enum.GetName(typ, i);
        string desc = name;
        FieldInfo fi = typ.GetField(name);

        // Get description for enum element
        DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

        if (attributes.Length > 0)
        {
            string s = attributes[0].Description;

            if (!string.IsNullOrEmpty(s))
            {
                desc = s;
            }
        }

        list.Add(new KeyValuePair<string, int>(desc, i));
    }
    // NOTE: It is very important that DisplayMember and ValueMember are set before DataSource.
    //       If you do, this works fine, and the SelectedValue of the ComboBox will be an int
    //       version of the Enum.
    //       If you don't, it will be a KeyValuePair.
    cxbx.DisplayMember = "Key";
    cxbx.ValueMember = "Value";
    cxbx.DataSource = list;
}

并使用上述方法以这种方式将combobox绑定到myEnum:

setEnumValues(comboBox, typeof(myEnum));

现在问题是如何将我的组合框项目或值设置为特定的项目,如下所示:

combobox.SelectedValue = myEnum.value;

我的项目是Visual Studio 2010环境中的C#windows项目。

2 个答案:

答案 0 :(得分:1)

我复制了你的代码,我认为这就是你所拥有的,这对我来说非常好。

namespace WindowsFormsApplication1
{
    public class MyClass
    {
        public string Something { get; set; }
        public UserFrienlyEnum foo { get; set; }
    }
    public enum UserFrienlyEnum
    {
        [Description("it need spec training")]
        SPECIAL_TRAINING = 1,
        [Description("it need normal training")]
        NORMAL_TRAINING = 2,
        [Description("it need simple training")]
        SIMPLE_TRAINING = 3
    }
    public partial class Form1 : Form
    {


        private void Form1_Load(object sender, EventArgs e)
        {
            setEnumValues(this.comboBox1, typeof(UserFrienlyEnum));
            MyClass variable = new MyClass();
            variable.foo = UserFrienlyEnum.NORMAL_TRAINING;
            this.comboBox1.SelectedValue = (int)variable.foo;

        }

        public static void setEnumValues(ComboBox cxbx, Type typ)
        {
            if (!typ.IsEnum)
            {
                throw new ArgumentException("Only Enum types can be set");
            }

            List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();

            foreach (int i in Enum.GetValues(typ))
            {
                string name = Enum.GetName(typ, i);
                string desc = name;
                FieldInfo fi = typ.GetField(name);

                // Get description for enum element
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attributes.Length > 0)
                {
                    string s = attributes[0].Description;
                    if (!string.IsNullOrEmpty(s))
                    {
                        desc = s;
                    }
                }

                list.Add(new KeyValuePair<string, int>(desc, i));
            }
            // NOTE: It is very important that DisplayMember and ValueMember are set before DataSource.
            //       If you do, this works fine, and the SelectedValue of the ComboBox will be an int
            //       version of the Enum.
            //       If you don't, it will be a KeyValuePair.
            cxbx.DisplayMember = "Key";
            cxbx.ValueMember = "Value";
            cxbx.DataSource = list;
        }

        public Form1()
        {
            InitializeComponent();
        }


    }
}

结果:

enter image description here

答案 1 :(得分:0)

感谢我的朋友Hanlet,这是最简单的答案:

combobox.SelectedValue = (int) myEnum.value;

将它转换为int是关键。

相关问题