【问题标题】:Sorting ObservableCollection by date按日期排序 ObservableCollection
【发布时间】:2016-10-22 05:52:07
【问题描述】:

我有 ObservableCollection AllRem

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; 
    ...
}

我需要按 ReminderDateTime 对我的收藏进行排序。我尝试使用 :IComparable 但它不起作用。

【问题讨论】:

  • 你试过 linq - var sortedCollection = yourCollection.OrderBy(x => x.ReminderDateTime)
  • 不会返回可观察的集合
  • 按照 Romasz 的建议进行操作,然后将其转换为可观察的集合 stackoverflow.com/questions/731626/…

标签: c# windows uwp


【解决方案1】:

如果集合中的数据没有改变,那么如上所述在绑定之前对集合进行排序就可以正常工作。如果是(而且,从您的数据结构来看,我猜是这样),那么这将成为 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 绑定到 CollectionViewSource

<GridView ItemsSource="{Binding Source ={StaticResource RemViewSource}}"></GridView>

【讨论】:

  • 是的,我需要更改收藏。但我不明白如何使用 CollectionViewSource。我有绑定到我的集合 ItemsSource="{x:Bind DS.AllRem, Mode=OneWay}" 的 GridView 我需要在哪里使用 CollectionViewSource?
  • 基本上结构变成了 CollectionViewSource.Source 绑定到 ViewModel 属性 GridView.ItemSource 绑定到 CollectionViewSource。我将编辑我的答案以提供完整的结构
  • 我有错误消息在“CollectionViewSource”类型中找不到属性“IsLiveSortingRequested”以及与此属性相关的其他相关内容
  • 您需要一个命名空间引用。将以下内容放入您的 UserControl/view 声明 xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
  • 没有帮助。 UWP不支持可以吗?
猜你喜欢
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 2017-07-05
  • 2016-07-26
  • 2014-09-07
  • 2018-01-25
  • 1970-01-01
  • 2020-12-06
相关资源
最近更新 更多