简体   繁体   English

Combobox充满了Enum

[英]Combobox filled with Enum

How can I bind a specific Enum in a Combobox ? 如何在Combobox绑定特定的Enum

public enum EduTypePublicEnum
  {
    [RMSEnumItem("1", "Properties.Resources.SEduAlumn")]
    Alumn,
    [RMSEnumItem("2", "Properties.Resources.SEduProfesor")]
    Profesor,
    [RMSEnumItem("3", "Properties.Resources.SEduAll")]
    All
  }

  public class EduTypePublic : RMSEnum<EduTypePublicEnum> { };

In my Form 在我的表格中

public EduAvisosForm()
{
     InitializeComponent();

     this.myComboBox.DataSource = Edu.Consts.EduTypePublic.Enums;
     this.myComboBox.DisplayMember = "Alumn";
     this.myComboBox.ValueMember = "Alumn";
}

But, with or without the ValueMember occurs an error. 但是,无论有没有ValueMember出现错误。 When I put this code without the ValueMember occurs an error asking for the ValueMember when I put it, doesn't work. 当我把这个代码放在没有ValueMember发生了一个错误,当我把它放在它上面时,要求ValueMember不起作用。

"Is not possible define SelectedValue in a ListControl with empty ValueMember"

public abstract class RMSEnum<TEnumType>
    {
        protected RMSEnum();

        public static string CodeList { get; }
        public static string[] Codes { get; }
        public static string DescriptionList { get; }
        public static string[] Descriptions { get; }
        public static object[] Enums { get; }

        public static string Code(TEnumType value);
        public static string Description(string code);
        public static string Description(TEnumType value);
        public static TEnumType Enum(string code);
    }

Your data source items should have properties which you specified as display and value members. 您的数据源项应具有您指定为显示和值成员的属性。 Eg dictionary KeyValuePairs have properties named Key and Value: 例如,字典KeyValuePairs具有名为Key和Value的属性:

 this.myComboBox.DisplayMember = "Value";
 this.myComboBox.ValueMember = "Key";
 this.myComboBox.DataSource = 
      Enum.GetValues(typeof(EduTypePublicEnum))
          .Cast<EduTypePublicEnum>()
          .Select(e => new { 
               Key = e.ToString(), // possibly read localized string
               Value = e
           }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM