繁体   English   中英

用于列表框控件的SortedOrder枚举

[英]SortedOrder Enum for listbox control

我有一个包含以下内容的枚举(例如):

clmn=543, 
tclk=432,   , 
tolf=876, 
frol=654 

我必须将此枚举键按排序顺序绑定到ListBox控件的ItemsSource属性。 枚举应按值排序。

请帮忙

MemberInfo[] memberInfos = typeof(MyEnum)
                          .GetMembers(BindingFlags.Public | BindingFlags.Static);

mylistBox.ItemSource =  memberInfos.Select(x => x.Name).ToArray(); 

'对枚举进行排序的代码:

   public static SortedList<int, string> GetEnumDataSource<T>()
    {
        Type myEnumType = typeof(T);
        SortedList<int, string> returnCollection = new SortedList<int, string>();
        try
        {
            if (myEnumType.BaseType == typeof(Enum))
            {
                string[] enumNames = Enum.GetNames(myEnumType);
                int enumLength = enumNames.Length - 1;
                for (int i = 0; i <= enumLength; i++)
                {
                    returnCollection.Add(Convert.ToInt32(Enum.Parse(myEnumType, enumNames[i])), enumNames[i]);
                }
            }
        }
        catch (Exception exception1)
        {

            return null;
        }
        return returnCollection;
    }

'将枚举绑定到下拉列表框的代码:

public void BindBox()
{
    SortedList<int, string> phoneTypes = null;
    phoneTypes = GetEnumDataSource<PhoneNumberType>();

    if ((phoneTypes != null)) {
        dropdownctrl.DataSource = phoneTypes;
        dropdownctrl.DataValueField = "Key";
        dropdownctrl.DataTextField = "Value";
        dropdownctrl.DataBind();
    }
}

该代码获取MyEnum的值并对它们进行排序,希望对您有所帮助。

var values = typeof(MyEnum).GetEnumValues();
Array.Sort(values);

暂无
暂无

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

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