接着介绍一下其使用方法。
System.Windows.Controls.Data(分页器控件DataPager所在DLL)
XAML文件头部名空间声明:
然后我们要在当前XAML中放置一个DataGrid用于显示列表数据,声明如下:
为了演示不同显示样式(DisplayMode)的效果,下面根据其样式分别加以声明:
HorizontalAlignment="Left" Source="{Binding}" NumericButtonCount="3" AutoEllipsis="True"/>
<data:DataPager x:Name ="FirstLastPreviousNext" Grid.Column="0" Grid.Row="3" DisplayMode="FirstLastPreviousNext"
Margin="0,0,0,4" HorizontalAlignment="Left" Source="{Binding}" NumericButtonCount="3" AutoEllipsis="True"/>
<data:DataPager x:Name ="FirstLastPreviousNextNumeric" Grid.Column="0" Grid.Row="4" DisplayMode="FirstLastPreviousNextNumeric"
Margin="0,0,0,4" HorizontalAlignment="Left" Source="{Binding}" NumericButtonCount="4" AutoEllipsis="True"/>
<data:DataPager x:Name ="Numeric" Grid.Column="0" Grid.Row="5" DisplayMode="Numeric" HorizontalAlignment="Left"
Margin="0,0,0,4" Source="{Binding}" NumericButtonCount="6" AutoEllipsis="False"/>
<data:DataPager x:Name ="PreviousNext" Grid.Column="0" Grid.Row="6" DisplayMode="PreviousNext" Margin="0,0,0,4"
HorizontalAlignment="Left" Source="{Binding}" NumericButtonCount="3" AutoEllipsis="False" />
<data:DataPager x:Name ="PreviousNextNumeric" Grid.Column="0" Grid.Row="7" DisplayMode="PreviousNextNumeric" Margin="0,0,0,4"
HorizontalAlignment="Left" Source="{Binding}" NumericButtonCount="4" AutoEllipsis="False" />
好了,为了便于演示,我“硬编码”了一个数据对象集合如下:
{
List<EmployeeInfo> employeeList = new List<EmployeeInfo>();
employeeList.Add(new EmployeeInfo { EmployeeID = 1, EmployeeName = "大林", Salary = 1000, City = "合肥" });
employeeList.Add(new EmployeeInfo { EmployeeID = 2, EmployeeName = "小林", Salary = 1000, City = "合肥" });
employeeList.Add(new EmployeeInfo { EmployeeID = 3, EmployeeName = "张三", Salary = 1000, City = "合肥" });
employeeList.Add(new EmployeeInfo { EmployeeID = 4, EmployeeName = "李四", Salary = 1500, City = "天津" });
employeeList.Add(new EmployeeInfo { EmployeeID = 5, EmployeeName = "王五", Salary = 2000, City = "上海" });
employeeList.Add(new EmployeeInfo { EmployeeID = 6, EmployeeName = "赵六", Salary = -800, City = "北京" });
employeeList.Add(new EmployeeInfo { EmployeeID = 7, EmployeeName = "尤七", Salary = 2100, City = "武汉" });
employeeList.Add(new EmployeeInfo { EmployeeID = 8, EmployeeName = "马八", Salary = 2300, City = "海口" });
employeeList.Add(new EmployeeInfo { EmployeeID = 9, EmployeeName = "许九", Salary = 2300, City = "海口" });
employeeList.Add(new EmployeeInfo { EmployeeID = 10, EmployeeName = "代十", Salary = 2300, City = "海口" });
return employeeList;
}
public class EmployeeInfo
{
public int EmployeeID { set; get; }
public string EmployeeName { set; get; }
public int Salary { set; get; }
public int[] Cost { get; set; }
public string City { set; get; }
}
接下来就是如何实现分页,以及相关控件的数据绑定了,代码位于MyDataPager.xaml.cs(详见注释):
public MyDataPager()
{
InitializeComponent();
PagedCollectionView pcv = new PagedCollectionView(GetEmployeeList());
pcv.PageSize = 3;//一页显示三条记录
//列表控件数据源绑定
MyDataGrid.DataContext = pcv;
MyListBox.DataContext = pcv;
//分页控件数据源绑定
FirstLastNumeric.DataContext = pcv;
FirstLastPreviousNext.DataContext = pcv;
FirstLastPreviousNextNumeric.DataContext = pcv;
Numeric.DataContext = pcv;
PreviousNext.DataContext = pcv;
PreviousNextNumeric.DataContext = pcv;
}
是不是很好用,呵呵。当然该控件还提供了NumericButtonCount属性用于控制页码的数量,这在上面的XAML控件声明时已使用,大家可以修改测试一下效果。