【问题标题】:WPF DataGrid PaginationWPF 数据网格分页
【发布时间】:2014-10-07 15:19:53
【问题描述】:

我正在使用此处提供的示例StackOverflow related question,如果我在网格中有偶数个项目,那么它工作得很好,但如果我有一个像 7 个项目的奇数,它会抛出一个超出范围我通过添加此行修复的异常

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 页看起来不错,但最后一页重复最后一个项目两次。像这样

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

【问题讨论】:

    标签: c# wpf datagrid pagination


    【解决方案1】:

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

        public override object GetItemAt(int index)
        {
            var offset = index % (this._itemsPerPage); 
    
            return this._innerList[this.StartIndex + offset];
        }
    

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

        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; 
            }
        }
    

    【讨论】:

    • 另外,如果你想支持一个空的数据集,你需要在 Count 覆盖的开头进行这样的检查:“if (_innerList.Count == 0) return 0;”
    • 好吧,我想实现排序@Yushatak。当 itemsource 绑定到分页的 collectionview 时,datagrid 的默认排序不知何故不起作用。
    • 排序不起作用,因为public override object GetItemAt(int index) 方法从单独维护的_internalList 中获取项目。基类对项目进行了正确排序(您可以在调试期间看到),但 PagingCollectionView 没有使用这些项目。不幸的是,基类的内部列表不受保护,因此不可能编写一个像样的子类。我觉得奇怪的是 WPF 不支持这个开箱即用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 2021-12-17
    • 2015-02-20
    • 2011-08-16
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多