2017-04-17 134 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); 

这对我有效。 希望你也是。

相关问题