WPF DataGrid 分页 [英] WPF DataGrid Pagination

查看:28
本文介绍了WPF DataGrid 分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此处提供的示例 StackOverflow 相关问题,如果我在网格中有偶数个项目,那么它一切正常,但是如果例如我有一个像 7 个项目这样的奇数,它会抛出一个超出范围的异常,我通过添加这一行来修复

I am using the example provided here StackOverflow related question, if i have an even number of items in the grid then it works all good, but if for instance i have an odd number like 7 items, it throws a out of range exception which I fixed by adding this line

public override object GetItemAt(int index)
{
    var offset = ((index % (this._itemsPerPage)) + this.StartIndex) > this._innerList.Count - 1 ? 0 : index % (this._itemsPerPage);
    return this._innerList[this.StartIndex + offset];
}

问题是在修复此问题后,如果您将每页的项目数设置为 2,那么您将有 4 页,前 3 页看起来正确,但最后一个重复了最后一个项目两次.像这样

The problem is that after fixing this, if you set the items per page to 2 then you will have 4 pages, the first 3 pages look right but the last one repeats the last item twice. like this

我是 WPF 的新手,我不确定如何处理这件作品,我不明白为什么它会重复该项目.

I am new to WPF and i am not sure how i can handle this piece, i do not understand why it would repeat the item.

推荐答案

问题不在于 GetItemAt 方法,保持原样:

The issue is not with GetItemAt method, leave it as it was:

    public override object GetItemAt(int index)
    {
        var offset = index % (this._itemsPerPage); 

        return this._innerList[this.StartIndex + offset];
    }

问题在于 Count 属性覆盖.如果它是最后一页,它应该返回正确的项目:

The problem is with Count property override. In case it is the last page it should return the correct items left:

    public override int Count
    {
        get 
        {
            //all pages except the last
            if (CurrentPage < PageCount)
                return this._itemsPerPage;

            //last page
            int remainder = _innerList.Count % this._itemsPerPage;

            return remainder == 0 ? this._itemsPerPage : remainder; 
        }
    }

这篇关于WPF DataGrid 分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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