为了完成ListView分页操作,本文会封装一个负责“分页操作”的泛型类PageInfo,该泛型类不仅适用于WPF中ListView的分页,还适用于WPF及WinForms中其他需要分页操作的控件。
下面是PageInfo类的完整代码(包含枚举类型JumpOperation的定义)。
public class PageInfo
{
public List dataSource;
int pageSize;
int pagecount;
int currentIndex;
public PageInfo()
{
currentIndex = 1;
}
public PageInfo(List dataSource, int pageSize)
: this()
{
this.dataSource = dataSource;
this.pageSize = pageSize;
this.pagecount = dataSource.Count / pageSize;
this.pagecount += (dataSource.Count % pageSize) != 0 ? 1 : 0;
}
public List GetPageData(JumpOperation jo)
{
switch (jo)
{
case JumpOperation.GoHome:
currentIndex = 1;
break;
case JumpOperation.GoPrePrevious:
if (currentIndex > 1)
{
currentIndex -= 1;
}
break;
case JumpOperation.GoNext:
if (currentIndex < pagecount)
{
currentIndex += 1;
}
break;
case JumpOperation.GoEnd:
currentIndex = pagecount;
break;
}
List listPageData = new List();
try
{
int pageCountTo = pageSize;
if (pagecount == currentIndex && dataSource.Count % pageSize > 0)
{
pageCountTo = dataSource.Count % pageSize;
}
if (null != dataSource)
{
for (int i = 0; i < pageCountTo; i++)
{
if ((currentIndex - 1) * pageSize + i < dataSource.Count)
{
listPageData.Add(dataSource[(currentIndex - 1) * pageSize + i]);
}
else
{
break;
}
}
}
return listPageData;
}
catch
{
return listPageData;
}
}
}
public enum JumpOperation
{
GoHome = 0,
GoPrePrevious = 1,
GoNext = 2,
GoEnd =3
}
实例化PageInfo类时,传入ListView需要的数据源及每页显示的记录数,进而计算出总页数,并将当前页设置为1。
PageInfo类提供的GetPageData方法可以用来获取第一页,最后一页,前一页及后一页的数据源。
下面来演示PageInfo类的使用方法。
(1)前端页面代码
(2)前端页面的逻辑代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ListViewSplitPages
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
List studentList;
PageInfo pageStudentList;
public MainWindow()
{
InitializeComponent();
studentList = new List() {
new Student(){ID="00000001",Name ="zsd",Class="1",Age="17"},
new Student(){ID="00000002",Name ="fds",Class="3",Age="15"},
new Student(){ID="00000003",Name ="ds",Class="1",Age="17"},
new Student(){ID="00000004",Name ="fd",Class="3",Age="16"},
new Student(){ID="00000005",Name ="rw",Class="1",Age="17"},
new Student(){ID="00000006",Name ="hg",Class="1",Age="15"},
new Student(){ID="00000007",Name ="dfd",Class="2",Age="16"},
new Student(){ID="00000008",Name ="dfd",Class="2",Age="17"},
new Student(){ID="00000009",Name ="gdf",Class="2",Age="17"},
new Student(){ID="00000010",Name ="jh",Class="1",Age="15"},
new Student(){ID="00000011",Name ="fdd",Class="2",Age="17"},
new Student(){ID="00000012",Name ="rer",Class="1",Age="17"},
new Student(){ID="00000013",Name ="vvv",Class="1",Age="15"},
new Student(){ID="00000014",Name ="qww",Class="3",Age="16"},
new Student(){ID="00000015",Name ="sds",Class="3",Age="18"},
new Student(){ID="00000016",Name ="lk",Class="1",Age="17"},
new Student(){ID="00000017",Name ="ui",Class="2",Age="16"},
new Student(){ID="00000018",Name ="jkj",Class="1",Age="17"},
};
pageStudentList = new PageInfo(studentList, 5);
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoHome);
}
private void Button_Home_Click(object sender, RoutedEventArgs e)
{
this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoHome);
}
private void Button_Previous_Click(object sender, RoutedEventArgs e)
{
this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoPrePrevious);
}
private void Button_Next_Click(object sender, RoutedEventArgs e)
{
this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoNext);
}
private void Button_End_Click(object sender, RoutedEventArgs e)
{
this.ListView_Students.ItemsSource = pageStudentList.GetPageData(JumpOperation.GoEnd);
}
}
public class Student
{
public string ID { get; set; }
public string Name { get; set; }
public string Class { get; set; }
public string Age { get; set; }
}
}
代码中,使用学生集合作为测试数据源,所以泛型类PageInfo的类型参数T被设置为Student,很显然,T还可以是其他的任何类型。
当点击画面中的“首页”、“前一页”、“下一页”、“尾页”便会调用PageInfo类型的GetPageData方法来获取数据源。
下面是程序运行后的画面
小编为了学习,加深印象而复制别人的文章过来,这是原来作者的文章链接:https://blog.csdn.net/yl2isoft/article/details/51228290