自己的 CollectionView 用于分页、排序和过滤 [英] Own CollectionView for paging, sorting and filtering

查看:16
本文介绍了自己的 CollectionView 用于分页、排序和过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了自己的 CollectionView 来将数据集合绑定到 WPF 中的 DataGrid.

I've implemented my own CollectionView to bind a collection of data to a DataGrid in WPF.

主要目标是分页,效果很好.我编写了以下 C# 代码:

The main goal was the pagination, which is working quite well. I've written the following C# code:

public class SchemesCollectionView : CollectionView
{
    private readonly IList<Scheme> innerList;
    private readonly int itemsPerPage;

    private int currentPage = 1;

    public SchemesCollectionView(IList<Scheme> source, int itemsPerPage)
        : base(source)
    {
        innerList = source;
        this.itemsPerPage = itemsPerPage;
    }

    public override int Count
    {
        get { return itemsPerPage; }
    }

    public int CurrentPage
    {
        get { return currentPage; }
        set
        {
            currentPage = value;
            OnPropertyChanged(new PropertyChangedEventArgs("CurrentPage"));
            OnPropertyChanged(new PropertyChangedEventArgs("FirstItemNumber"));
            OnPropertyChanged(new PropertyChangedEventArgs("LastItemNumber"));
        }
    }

    public int ItemsPerPage { get { return this.itemsPerPage; } }

    public int PageCount
    {
        get
        {
            return (this.innerList.Count() + this.itemsPerPage - 1)
                / this.itemsPerPage;
        }
    }

    public int LastItemNumber
    {
        get
        {
            var end = currentPage * itemsPerPage - 1;
            end = (end > innerList.Count()) ? innerList.Count() : end;

            return end + 1;
        }
    }

    public int StartIndex
    {
        get { return (currentPage - 1) * itemsPerPage; }
    }

    public int FirstItemNumber
    {
        get { return ((currentPage - 1) * itemsPerPage) + 1; }
    }

    public override object GetItemAt(int index)
    {
        var offset = index % (ItemsPerPage);

        var position = StartIndex + offset;

        if (position >= innerList.Count)
        {
            position = innerList.Count - 1;
        }

        return innerList[position];
    }

    public void MoveToNextPage()
    {
        if (CurrentPage < PageCount)
        {
            CurrentPage += 1;
        }
        Refresh();
    }

    public void MoveToPreviousPage()
    {
        if (CurrentPage > 1)
        {
            CurrentPage -= 1;
        }
        Refresh();
    }

    public void MoveToFirstPage()
    {
        CurrentPage = 1;
        Refresh();
    }

    public void MoveToLastPage()
    {
        CurrentPage = PageCount;
        Refresh();
    }
}

如前所述,分页效果很好.但是我无法进行过滤和排序工作.当我向 Filter 属性添加自定义过滤器时,它被完全忽略.排序也是一样.单击后可以看到列标题上的箭头,但不同的排序未反映在 DataGrid 中.

As mentioned, the pagination works very well. But I can't get the filtering and sorting work. When I add a custom filter to the Filter property, It gets completely ignored. The same with the sorting. I can see the arrows on the column headers after I clicked them, but the different sorting is not reflected within the DataGrid.

我在这里缺少什么?希望有人能帮忙.

What I'm missing here? Hope someone can help.

推荐答案

如本文所述,您可以从 Silverlight 获取代码并在 WPF 中使用.

As mentioned in this, you can take code from Silverlight and use that in WPF.

WPF 中的分页集合视图

这篇关于自己的 CollectionView 用于分页、排序和过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆