2017-04-17 7 views
0

私は、観察可能なコレクションをソートする方法を見つけるのに少し問題があります。私は現在、観察可能なコレクションを表示しているリストビューをリアルタイムで変更するために押されたボタンからのイベントを使用してこのプロセスをテストしようとしており、「ソート」コマンドを使用できないことを理解していますが、 "OrderBy"コマンド。私のコードは現在次のようになっています:UWP Visual Studio 2017 ObservableCollectionソート

public sealed partial class MainPage : Page 
{ 

    ObservableCollection<DataType> collection = new ObservableCollection<DataType>(); 

    public MainPage() 
    { 
     this.InitializeComponent(); 

     setupCollection(); 
    } 

    public void setupCollection() 
    { 
     collection.Add(new DataType { times = "08:30" }); 
     collection.Add(new DataType { times = "00:30" }); 
     collection.Add(new DataType { times = "12:30" }); 
     collection.Add(new DataType { times = "23:30" }); 
     collection.Add(new DataType { times = "18:30" }); 
     collection.Add(new DataType { times = "15:30" }); 
     collection.Add(new DataType { times = "06:30" }); 
     collection.Add(new DataType { times = "05:30" }); 
     collection.Add(new DataType { times = "14:00" }); 
     collection.Add(new DataType { times = "12:00" }); 
     listview.ItemsSource = collection; 
    } 

    public class DataType 
    { 
     public string times { get; set; } 
    } 

    private void Button_Tapped(object sender, TappedRoutedEventArgs e) 
    { 
     collection = new ObservableCollection<DataType>(from i in DataType orderby i.times select i); 
     //collection.OrderBy(i.DataType > i.times); 
    } 
} 

誰かが私のコードを修正してアイテムを注文する方法を知っていますか?

+0

どのようにOrderByを動作させることができますか? –

+0

WPFでは、 'CollectionViewSource'を使用してこれを行うのは簡単ではないことに注意してください。 UWPで同様のことを達成するためのアイデアについては、https://stackoverflow.com/questions/34915276/uwp-observablecollection-sorting-and-groupingを参照してください。 –

答えて

3

問題がある:あなたがクラスのデータ型で検索されていることを、あなたは値を取り、順番にそれらを置くことができますので、コレクションに検索する必要があります...ここソリューションです:

collection = new ObservableCollection<DataType>(
    from i in collection orderby i.times select i); 

それは私のために働いた。 あなたもお待ちしています。

関連する問題