0

私は以下を含む列挙型を持っています(例えば):

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

この列挙型キーを、並べ替えられた順序でListBoxコントロールのItemsSourceプロパティにバインドする必要があります。列挙型は値でソートする必要があります。

助けてください

4

3 に答える 3

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

mylistBox.ItemSource =  memberInfos.Select(x => x.Name).ToArray(); 
于 2012-05-15T10:09:19.810 に答える
0

'列挙型をソートするコード:

   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();
    }
}
于 2012-05-15T10:11:49.417 に答える
0

このコードはMyEnumの値を取得して並べ替えます。お役に立てば幸いです。

var values = typeof(MyEnum).GetEnumValues();
Array.Sort(values);
于 2012-05-15T10:39:16.830 に答える