【问题标题】:Pagination after filtering on CollectionViewSource在 CollectionViewSource 上过滤后的分页
【发布时间】:2017-06-01 07:09:07
【问题描述】:

我有一个问题。我有一个非常简单的分页机制,结合了 WPF 按钮和数据网格,它看起来像:

    public CatalogViewModel()
    {
        this._catalog = new CatalogContexct();

        DbQuery<Candidate> candidatesValues = this._catalog.Candidates.Include("commendation");
        this._totalItems = candidatesValues.Count();

        this._candidates = candidatesValues.ToArray()
            .OrderBy(c => c.Firstname).Skip(this.Start).Take(this._itemCount).ToList();

        this._filters = new Dictionary<string, Predicate<Candidate>>();            

        var candidates = new ListCollectionView(this._candidates);
        this.CandidateView = CollectionViewSource.GetDefaultView(candidates);

        this.FirstameCommand = new RelayCommand(FilterFirstname, param => this._canExecute);
        this.LastnameCommand = new RelayCommand(FilterLastname, param => 

        CandidateView.Filter = FilterCandidates;

        //should be something else to limit view to page so filter can be done on whole source not only part of it like above
    }

但正如我所描述的,我需要在分页之前过滤值或对 this.CandidateView = CollectionViewSource.GetDefaultView(candidates); 的过滤值进行分页。

有可能吗?

如果我手动过滤集合并将该值传递给查看,我会很顺利,但我想知道是否有比手动操作数据更简单的方法。

【问题讨论】:

    标签: c# wpf pagination


    【解决方案1】:

    您可以使用与过滤ICollectionView 相同的谓词过滤源集合:

    this._candidates = candidatesValues.Where(FilterCandidates).OrderBy(c => c.Firstname).Skip(this.Start).Take(this._itemCount).ToList();
    

    那么您根本不需要过滤甚至使用 ICollectionView。您可以直接绑定到过滤和分页的源集合。

    【讨论】:

      猜你喜欢
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      相关资源
      最近更新 更多