WPF DataGrid分页 [英] WPF DataGrid Pagination
问题描述
我正在使用此处提供的示例 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屋!