wpf collectionview 分页
时间: 2023-08-25 07:05:42 浏览: 92
WPF中的CollectionView可以用于集合数据的排序、过滤、分组等操作,其中也包含了分页的功能。
以下是使用CollectionView进行分页的示例代码:
```csharp
public class MyViewModel
{
private ObservableCollection<MyModel> _allItems;
public MyViewModel()
{
// 初始化数据集合
_allItems = new ObservableCollection<MyModel>();
for (int i = 0; i < 1000; i++)
{
_allItems.Add(new MyModel() { Name = $"Item {i}" });
}
// 初始化CollectionView
var view = new CollectionView(_allItems);
view.Filter = FilterMethod;
view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
view.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
// 分页
var pager = new Pager(view, 20);
PagerView = pager.View;
}
private bool FilterMethod(object obj)
{
var item = obj as MyModel;
return !string.IsNullOrEmpty(FilterText) ? item.Name.Contains(FilterText) : true;
}
private string _filterText;
public string FilterText
{
get { return _filterText; }
set
{
if (_filterText != value)
{
_filterText = value;
PagerView.Refresh();
}
}
}
public ICollectionView PagerView { get; private set; }
}
public class Pager
{
private readonly ICollectionView _view;
private readonly int _pageSize;
private int _currentPage;
public Pager(ICollectionView view, int pageSize)
{
_view = view;
_pageSize = pageSize;
_currentPage = 1;
_view.Filter = FilterMethod;
}
public ICollectionView View => _view;
public void MoveToPage(int page)
{
if (page < 1 || page > PageCount)
{
return;
}
_currentPage = page;
_view.Refresh();
}
private bool FilterMethod(object obj)
{
var index = _view.IndexOf(obj);
return index >= (_currentPage - 1) * _pageSize && index < _currentPage * _pageSize;
}
public int PageCount => (int)Math.Ceiling((double)_view.Count / _pageSize);
public int CurrentPage => _currentPage;
}
```
在ViewModel中初始化数据集合,并使用CollectionView对集合进行排序、过滤和分组。接着,创建一个Pager类来进行分页操作,该类包含一个MoveToPage方法来移动到指定页码,并在该方法中调用CollectionView的Refresh方法来更新数据。同时,在Pager类中实现了FilterMethod方法来进行分页过滤。
最后,在ViewModel中创建一个Pager对象,并将其View属性绑定到UI的ItemsSource属性上。在UI中,可以使用Pager对象的MoveToPage方法来移动到指定页码。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)