【发布时间】:2018-09-25 15:23:00
【问题描述】:
我在尝试找到一种对可观察集合进行排序的方法时遇到了一些麻烦。我目前正在尝试通过使用按钮中的事件来测试此过程,该按钮被按下以实时更改显示可观察集合的列表视图,我知道我不能使用“排序”命令,但我可以通过使用来完成此操作“OrderBy”命令。我目前的代码如下:
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);
}
}
有谁知道修复我的代码以便我可以订购其中的物品的方法?
【问题讨论】:
-
什么情况下不能让 OrderBy 工作?
-
请注意,在 WPF 中,使用
CollectionViewSource完成此操作很简单。有关在 UWP 中完成类似操作的想法,请参阅stackoverflow.com/questions/34915276/…
标签: c# uwp observablecollection visual-studio-2017