在Silverlight Toolkit 3.0中提供了强大灵活的数据分页控件:DataPager。并且其还有多种显示样式 (DisplayMode) 供使用。终于可以不用再自己费神巴力去实现分页功能了,下面是其演示效果:
接着介绍一下其使用方法。
接着介绍一下其使用方法。
首先,我们要新建一个Silverlight应用,并添加对下面DLL的引用:
System.Windows.Data(其提供了PagedCollectionView类用于实现分页绑定)
System.Windows.Controls.Data(分页器控件DataPager所在DLL)
System.Windows.Controls.Data(分页器控件DataPager所在DLL)
XAML文件头部名空间声明:
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
然后我们要在当前XAML中放置一个DataGrid用于显示列表数据,声明如下:
<
data:DataGrid
x:Name
="MyDataGrid"
Grid.Column
="0"
Grid.Row
="0"
ItemsSource
="
{Binding}
"
/>
为了演示不同显示样式(DisplayMode)的效果,下面根据其样式分别加以声明:
<
data:DataPager
x:Name
="FirstLastNumeric"
Grid.Row
="2"
DisplayMode
="FirstLastNumeric"
Margin
="0,0,0,4"
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" />
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" />
好了,为了便于演示,我“硬编码”了一个数据对象集合如下:
public
List
<
EmployeeInfo
>
GetEmployeeList()
{
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 ; }
}
{
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(详见注释):
using
System.Windows.Data;
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;
}
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控件声明时已使用,大家可以修改测试一下效果。
好的,今天的内容就先到这里了。
本文转自 daizhenjun 51CTO博客,原文链接:http://blog.51cto.com/daizhj/188781,如需转载请自行联系原作者