【问题标题】:UWP Visual Studio 2017 ObservableCollection sortingUWP Visual Studio 2017 ObservableCollection 排序
【发布时间】: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


【解决方案1】:

问题是:您在类 Datatype 中搜索,您需要在集合中搜索,以便获取值并将它们按顺序排列...这是解决方案:

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

这对我有用。 也希望你。

【讨论】:

    【解决方案2】:

    简单易行的方法:

    ConceptItems = new ObservableCollection<DataConcept>(ConceptItems.OrderBy(i => i.DateColumn));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      相关资源
      最近更新 更多