2017-06-20 8 views
0
私はコンボボックスでのObservableCollectionをソートしたい

、ソートなしソートのObservableCollection WPF

私の結果:

enter image description here

私の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 
    } 

どうすれば修正できますか?アルファベット順に注文することを強制する

codeTypeEnum = new ObservableCollection<IdentificationSystemType>(Enum.GetValues(typeof(IdentificationSystemType)) 
.Cast<IdentificationSystemType>().OrderBy(x => x.ToString())); 

:へ

codeTypeEnum = new ObservableCollection<IdentificationSystemType>(Enum.GetValues(typeof(IdentificationSystemType)) 
.Cast<IdentificationSystemType>()); 

: おかげで、

+0

どのようにソートしますか?アルファベット順ですか? – mjwills

+0

yes by order alphabetic – devtunis

+0

あなたは順序付けられたコレクションを割り当てられていません>> var ordered = displaySystemList.OrderBy(x => x); ' –

答えて

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列挙型の文字列名でしか作業しない場合。 Typiclyより良い(私の意見)列挙型オブジェクトを直接使用し、表示順序などのために正しくフォーマットします。その後、それを後で解析する必要はありません。 – NtFreX

+0

あなたは絶対に正しいです、それはそれをはるかに明確かつ維持しやすくします。解析の問題について考えなかった。 –

関連する問題