我创建了自己的CollectionView
,支持我的要求分页。我创建了一个继承自CollectionView
的版本,它在几乎所有场景中都可以正常运行。
现在,问题是当我 从列表中删除项目 时,它会按预期反映在用户界面中,但 中的项目视图未刷新 。我的意思是当我在视图中有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();
}
}
MainWindow.xaml
<StackPanel>
<ItemsControl ItemsSource="{Binding PagingSource}"/>
<Separator />
<ItemsControl ItemsSource="{Binding List}" />
<Button Width="150" Height="50" Click="Button_Click" Content="Remove an item from list" />
</StackPanel>
MainWindow.xaml.cs
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
。像这样的东西
CollectionChanged += PagingCollectionView_CollectionChanged;
private void PagingCollectionView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Refresh();
}
但结果是Stackoverflow Exception
:(。
有关如何在此方案中刷新CollectionView
的任何想法。
提前致谢。
答案 0 :(得分:-1)
在PagingCollectionView中添加它 - 您需要刷新集合:
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
{
base.OnCollectionChanged(args);
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
break;
case NotifyCollectionChangedAction.Remove:
this.Refresh();
break;
case NotifyCollectionChangedAction.Replace:
break;
case NotifyCollectionChangedAction.Move:
break;
case NotifyCollectionChangedAction.Reset:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
&#13;