我想在组合框中对ObservableCollection进行排序,
我的结果没有排序:
我的ViewModel:
private ObservableCollection<IdentificationSystemType> codeTypeEnum;
public IdentificationSystemType CodeType
{
get { return codeType; }
set { codeType = value;
OnPropertyChanged("CodeType");
}
}
public NewIdentificationSystemViewModel()
{
_identificationToAdd = new IdentificationSystem();
identificationDeviceToAdd = new IdentificationDevice();
_resetIdentificationCmd = new RelayCommand<string>(resetIdentification);
saveCommand = new RelayCommand<string>(addFunc, canSave);
codeTypeEnum = new ObservableCollection<IdentificationSystemType>(Enum.GetValues(typeof(IdentificationSystemType)).Cast<IdentificationSystemType>());
}
我曾尝试使用var ordered = codeTypeEnum.OrderBy(x => x);
,但没有任何东西..它是一样的
我的Enum声明:
public enum IdentificationTypes : int
{
TerminalEntryGate = 1,
TerminalExitGate = 2,
LoadingAreaEntryGate = 3,
LoadingAreaExitGate = 4,
IslandEntryGate = 5,
IslandExitGate = 6,
BayEntryGate = 7,
BayExitGate = 8,
ScalingAreaEntryGate = 9,
ScalingAreaExitGate = 10,
OfficeAreaEntryGate = 11,
OfficeAreaExitGate = 12,
TankFarmEntryGate = 13,
TankFarmExitGate = 14,
StagingAreaEntryGate = 15,
StagingAreaExitGate = 16,
LoadingBayIdentification = 21,
LoadingArmIdentification = 22,
LoadingIslandIdentification = 23,
PresetIdentification = 27
}
我该如何解决? 感谢,
答案 0 :(得分:1)
变化:
codeTypeEnum = new ObservableCollection<IdentificationSystemType>(Enum.GetValues(typeof(IdentificationSystemType))
.Cast<IdentificationSystemType>());
为:
codeTypeEnum = new ObservableCollection<IdentificationSystemType>(Enum.GetValues(typeof(IdentificationSystemType))
.Cast<IdentificationSystemType>().OrderBy(x => x.ToString()));
强制按字母顺序排序。
答案 1 :(得分:1)
由于您的枚举属于int类型,因此您可以按这些数字来订购您的收藏。如果要按字母顺序对集合进行排序,则需要先将整数解析为字符串。
您可以在提供OrderBy
方法的键选择器功能中执行此操作。
var values = Enum.GetValues(typeof(IdentificationTypes)).Cast<IdentificationTypes>();
var valueList = new ObservableCollection<IdentificationTypes>(values);
var orderedList = valueList.OrderBy(x => x.ToString());