私は以下を含む列挙型を持っています(例えば):
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);