如何将枚举绑定到组合框

时间:2011-04-12 16:33:45

标签: c# winforms

我会使用组合框控件绑定枚举的值。

我写了这段代码:

cboPriorLogicalOperator.DataSource = Enum.GetValues(typeof(MyEnum))
    .Cast<MyEnum>()
    .Select(p => new { Key = (int)p, Value = p.ToString() })
    .ToList();

myComboBox.DisplayMember = "Value";
myComboBox.ValueMember = "Key";

效果很好,但我想知道是否有更简单的方法。

5 个答案:

答案 0 :(得分:25)

我觉得你的代码很漂亮!

唯一的改进是将代码放在扩展方法

修改

当我考虑它时,你想要做的是在定义中使用Enum,而不是扩展方法所需的枚举实例。

我找到了这个question,它很好地解决了这个问题:

public class SelectList
{
    // Normal SelectList properties/methods go here

    public static SelectList Of<T>()
    {
       Type t = typeof(T);
       if (t.IsEnum)
       {
           var values = from Enum e in Enum.GetValues(t)
                        select new { ID = e, Name = e.ToString() };
           return new SelectList(values, "Id", "Name");
       }
       return null;
    }
}

// called with 
var list = SelectList.Of<Things>();

只有您可能想要返回Dictionary<int, string>而不是SelectList,但您明白了。

<强> EDIT2:

这里我们使用一个代码示例来介绍您正在查看的案例。

public class EnumList
{
    public static IEnumerable<KeyValuePair<T, string>> Of<T>()
    {
        return Enum.GetValues(typeof (T))
            .Cast<T>()
            .Select(p => new KeyValuePair<T, string>(p, p.ToString()))
            .ToList();
    }
}

也许这个版本的密钥是int

public class EnumList
{
    public static IEnumerable<KeyValuePair<int, string>> Of<T>()
    {
        return Enum.GetValues(typeof (T))
            .Cast<T>()
            .Select(p => new KeyValuePair<int, string>(Convert.ToInt32(p), p.ToString()))
            .ToList();
    }
}

答案 1 :(得分:6)

为什么不使用:

myComboBox.DataSource  = Enum.GetValues(typeof(MyEnum))

答案 2 :(得分:1)

foreach (int r in Enum.GetValues(typeof(MyEnum))) 
{
     var item = new ListItem(Enum.GetName(typeof(MyEnum), r), r.ToString());
     ddl.Items.Add(item);
}

答案 3 :(得分:0)

我最近遇到了一个问题,我有一个可以为空的枚举属性,需要将它绑定到ComboBox。这是我提出的解决方案:

using System;
using System.Collections.Generic;

namespace ActivitySchedule.Model
{
    public class NullableEnum<T> where T : struct, IComparable
    {
        public string Display { get; private set; }

        public T? Value { get; private set; }

        public static implicit operator T?(NullableEnum<T> o)
        {
            return o.Value;
        }

        public static implicit operator NullableEnum<T>(T? o)
        {
            return new NullableEnum<T>
            {
                Display = o?.ToString() ?? "NA",
                Value = o
            };
        }

        private NullableEnum() { }

        public static IEnumerable<NullableEnum<T>> GetList()
        {
            var items = new List<NullableEnum<T>>
            {
                new NullableEnum<T>
                {
                    Display = "NA",
                    Value = null
                }
            };

            var values = Enum.GetValues(typeof(T));

            foreach (T v in values)
            {
                items.Add(v);
            }

            return items;
        }
    }
}

我将对象包装在Controller类中并更改属性的Type,如下所示:

private MyClass myClass;

public NullableEnum<MyEnum> MyEnum
{
    get { return this.myClass.MyEnum; }
    set { this.myClass.MyEnum = value.Value; }
}

(它也可以是派生类并覆盖属性)

这就是我使用它的方式:

var types = NullableEnum<MyEnum>.GetList();
this.comboBox1.DataSource = types;
this.comboBox1.DisplayMember = "Display";
this.comboBox1.ValueMember = "Value";
this.comboBox1.Bindings.Add("SelectedValue", myClassController, "MyEnum");

答案 4 :(得分:-1)

private void Form1_Load(object sender, EventArgs e)
{
    comboBox1.DataSource = Enum.GetValues( typeof(Gender));
    Array gen = Enum.GetValues(typeof(Gender));
    List<KeyValuePair<string, char>> lstgender = new List<KeyValuePair<string,char>>();
    foreach(Gender g in gen)
        lstgender.Add(new KeyValuePair<string,char>(g.ToString(),((char)g)));
    comboBox1.DataSource = lstgender;
    comboBox1.DisplayMember = "key";
    comboBox1.ValueMember = "value"
}

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(comboBox1.SelectedValue.ToString());
}

public class Student
{
    public string stud_name { get; set; }
    public Gender stud_gen { get; set; }
}

public enum Gender
{
    Male='M',
    Female='F'
}