2017-06-20 41 views
0

我想設置分類保存在組合框中的ObservableCollection,排序的ObservableCollection的WPF

我的結果,而不進行排序:

enter image description here

我的視圖模型:

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);但沒有..它是一樣的 我枚舉聲明:

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

你怎麼想它排序?按字母順序? – mjwills

+0

是按訂單字母 – devtunis

+0

你還沒有指定有序集合>>'VAR下令= displaySystemList.OrderBy(X => X);' –

回答

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

當你的枚舉的類型爲int你被這些數字訂購您的收藏。如果你想按字母順序排列你的集合,你需要先將整數解析爲字符串。

您可以在鍵選擇功能,你給了OrderBy方法做到這一點。

var values = Enum.GetValues(typeof(IdentificationTypes)).Cast<IdentificationTypes>(); 
var valueList = new ObservableCollection<IdentificationTypes>(values); 
var orderedList = valueList.OrderBy(x => x.ToString()); 
+0

既然你提到的值不是它應該是'Enum.GetNames'而不是' Enum.GetValues'? –

+0

@ m.rogalski如果您只想使用枚舉的字符串名稱。一般來說,它更好(我的意見)直接使用枚舉對象,並正確地格式化顯示順序等等。然後,你不必在事後解析它。 – NtFreX

+0

你是絕對正確的,這使得它更容易維護。沒有想過解析問題。 –