C# 按日期对可观察到的收集进行排序
我有明显的收集记录C# 按日期对可观察到的收集进行排序,c#,windows,uwp,C#,Windows,Uwp,我有明显的收集记录 public sealed class GetRem { public static string RemIDForNot; public string ReminderColor = "1"; public int RemID { get; set; } public string ReminderName { get; set; } public string ReminderDescription { get; set; }
public sealed class GetRem
{
public static string RemIDForNot;
public string ReminderColor = "1";
public int RemID { get; set; }
public string ReminderName { get; set; }
public string ReminderDescription { get; set; }
public DateTime ReminderDataTime { get; set; }
public Boolean? ReminderDone = false;
...
}
我需要按提醒日期对我的收藏进行分类。我试着使用:IComparable,但不起作用 如果集合中的数据没有更改,则在绑定之前对集合进行排序(如上所述)可以正常工作。如果是(从您的数据结构来看,我想是这样),那么对于CollectionViewSource来说,这将是一个很好的场景
<UserControl.Resources>
<CollectionViewSource Source="{Binding DS.AllRem}" x:Key="RemViewSource" IsLiveSortingRequested="True" >
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="ReminderDataTime" Direction="Descending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<GridView ItemsSource="{Binding Source ={StaticResource RemViewSource}}"></GridView>
然后,将GridView绑定到CollectionViewSource
<UserControl.Resources>
<CollectionViewSource Source="{Binding DS.AllRem}" x:Key="RemViewSource" IsLiveSortingRequested="True" >
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="ReminderDataTime" Direction="Descending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<GridView ItemsSource="{Binding Source ={StaticResource RemViewSource}}"></GridView>
如果集合中的数据没有更改,则在绑定之前按照上面的建议对集合进行排序可以正常工作。如果是(从您的数据结构来看,我想是这样),那么对于CollectionViewSource来说,这将是一个很好的场景
<UserControl.Resources>
<CollectionViewSource Source="{Binding DS.AllRem}" x:Key="RemViewSource" IsLiveSortingRequested="True" >
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="ReminderDataTime" Direction="Descending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<GridView ItemsSource="{Binding Source ={StaticResource RemViewSource}}"></GridView>
然后,将GridView绑定到CollectionViewSource
<UserControl.Resources>
<CollectionViewSource Source="{Binding DS.AllRem}" x:Key="RemViewSource" IsLiveSortingRequested="True" >
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="ReminderDataTime" Direction="Descending"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<GridView ItemsSource="{Binding Source ={StaticResource RemViewSource}}"></GridView>
您是否尝试过使用linq-var sortedCollection=yourCollection.OrderBy(x=>x.rementerDateTime)
?它不会返回可观察的集合按照Romasz的建议执行,然后将其转换为可观察的集合您尝试过使用linq-var sortedCollection=yourCollection.OrderBy(x=>x.rementerDateTime)吗
?不会返回可观察的集合按照Romasz的建议执行,然后将其转换为可观察的集合是的,我需要更改集合。但我不明白如何使用CollectionViewSource。我有绑定到我的集合项资源的GridView=“{x:Bind DS.AllRem,Mode=one-way}”,我需要在其中使用CollectionViewSource?基本上,该结构成为CollectionViewSource.Source绑定到ViewModel属性GridView.ItemSource绑定到CollectionViewSource。我将编辑我的答案以提供完整结构我有错误消息在类型“CollectionViewSource”和其他与此属性相关的属性“IsLiveSortingRequested”中找不到属性“IsLiveSortingRequested”。您需要命名空间引用。在用户控件/视图声明xmlns中放入以下内容:componentModel=“clr namespace:System.componentModel;assembly=WindowsBase”没有帮助。UWP不支持吗?是的,我需要更换收藏。但我不明白如何使用CollectionViewSource。我有绑定到我的集合项资源的GridView=“{x:Bind DS.AllRem,Mode=one-way}”,我需要在其中使用CollectionViewSource?基本上,该结构成为CollectionViewSource.Source绑定到ViewModel属性GridView.ItemSource绑定到CollectionViewSource。我将编辑我的答案以提供完整结构我有错误消息在类型“CollectionViewSource”和其他与此属性相关的属性“IsLiveSortingRequested”中找不到属性“IsLiveSortingRequested”。您需要命名空间引用。在用户控件/视图声明xmlns中放入以下内容:componentModel=“clr namespace:System.componentModel;assembly=WindowsBase”没有帮助。UWP是否可以不支持?