Wpf 如何在更改集合时手动刷新自定义集合视图?

Wpf 如何在更改集合时手动刷新自定义集合视图?,wpf,xaml,collectionviewsource,Wpf,Xaml,Collectionviewsource,我已经创建了自己的CollectionView,它支持为我的需求分页。我已经创建了一个继承自CollectionView的应用程序,它在几乎所有场景中都能像预期的那样完美工作 现在的问题是,每当我从列表中删除一个项目时,会像预期的那样反映在UI中,但是视图中的项目不会刷新。我的意思是当我在视图中有5个项目时,如果我从列表中删除第4个项目,第6个项目不会显示在页面上,现在只显示4个项目 这是密码 PagingCollectionView.cs public class PagingCollecti

我已经创建了自己的
CollectionView
,它支持为我的需求分页。我已经创建了一个继承自
CollectionView
的应用程序,它在几乎所有场景中都能像预期的那样完美工作

现在的问题是,每当我从列表中删除一个项目时,会像预期的那样反映在UI中,但是视图中的项目不会刷新。我的意思是当我在视图中有5个项目时,如果我从列表中删除第4个项目,第6个项目不会显示在页面上,现在只显示4个项目

这是密码

PagingCollectionView.cs

public class PagingCollectionView : CollectionView
{
    private readonly IList _innerList;
    private readonly int _itemsPerPage;

    private int _currentPage = 1;

    public PagingCollectionView(IList innerList, int itemsPerPage)
        : base(innerList)
    {
        this._innerList = innerList;
        this._itemsPerPage = itemsPerPage;            
    }     

    public override int Count
    {
        get
        {
            if (this._currentPage < this.PageCount) // page 1..n-1
            {
                return this._itemsPerPage;
            }
            else // page n
            {
                var itemsLeft = this._innerList.Count % this._itemsPerPage;
                if (0 == itemsLeft)
                {
                    return this._itemsPerPage; // exactly itemsPerPage left
                }
                else
                {
                    // return the remaining items
                    return itemsLeft;
                }
            }
        }
    }      

    public int CurrentPage
    {
        get { return this._currentPage; }
        set
        {
            this._currentPage = value;
            this.OnPropertyChanged(new PropertyChangedEventArgs("CurrentPage"));
        }
    }

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

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

    private int EndIndex
    {
        get
        {
            var end = this._currentPage * this._itemsPerPage - 1;
            return (end > this._innerList.Count) ? this._innerList.Count : end;
        }
    }

    private int StartIndex
    {
        get { return (this._currentPage - 1) * this._itemsPerPage; }
    }

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

        if ((this._innerList != null) && (this._innerList.Count > 0) && (iIndex >= 0) && (iIndex <= this._innerList.Count))
            return this._innerList[iIndex];
        else
            return new object();
    }

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

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

    public void MoveToPage(int pageno)
    {
        if (pageno > 0 && pageno <= this.PageCount)
        {
            this.CurrentPage = pageno;
        }
        this.Refresh();
    }

    public void RefreshCurrentPage()
    {
        this.Refresh();
    }
}
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        _list = new ObservableCollection<string> { "Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7" };            
        this.DataContext = this;
    }

    private ObservableCollection<string> _list;

    public ObservableCollection<string> List
    {
        get { return _list; }
        set { _list = value; }
    }

    private PagingCollectionView _pagingsource;      

    public PagingCollectionView PagingSource
    {
        get
        {
            _pagingsource = new PagingCollectionView(List, 5);                
            return _pagingsource;
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        _list.RemoveAt(2);
    }
}
但它会导致
堆栈溢出异常
:(

关于如何在此场景中刷新我的
CollectionView
的任何想法


提前感谢。

将此添加到PagingCollectionView中-您需要刷新收藏:

受保护的覆盖无效OnCollectionChanged(NotifyCollectionChangedEventArgs)
{
基础.更改的集合(args);
开关(参数操作)
{
案例NotifyCollectionChangedAction。添加:
打破
案例NotifyCollectionChangedAction。删除:
这个。刷新();
打破
案例通知收集更改操作。替换:
打破
案例通知收集更改操作。移动:
打破
案例通知CollectionChangedAction.Reset:
打破
违约:
抛出新ArgumentOutOfRangeException();
}
}
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        _list = new ObservableCollection<string> { "Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7" };            
        this.DataContext = this;
    }

    private ObservableCollection<string> _list;

    public ObservableCollection<string> List
    {
        get { return _list; }
        set { _list = value; }
    }

    private PagingCollectionView _pagingsource;      

    public PagingCollectionView PagingSource
    {
        get
        {
            _pagingsource = new PagingCollectionView(List, 5);                
            return _pagingsource;
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        _list.RemoveAt(2);
    }
}
    CollectionChanged += PagingCollectionView_CollectionChanged;         


    private void PagingCollectionView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Refresh();
    }