思路实现传统的分页功能,即从服务端获取到分页的记录集,再得到符合条件的记录集总数,然后构造一个数据集合,该数据集合数量等于查询结果总数并将其绑定到datapager控件,并且将得到的分页结果绑定到datagrid。当页面pageindex变换时,引发事件从服务端获取相应pageindex的记录集。在具体实现过程中,可以将datagrid和datapager控件封装成一个分页控件
customdatapager : datapager
{
private dependencyproperty m_pageindex = dependencyproperty.register("pagecount", typeof(int), typeof(customdatapager), new propertymetadata(propertychangedcallbackhandler));public new int pagecount
{
get
{
return (int)this.pageindex + 1;
}
set
{
this.setvalue(m_pageindex, value);
}
}private static void propertychangedcallbackhandler(dependencyobject d, dependencypropertychangedeventargs e)
{
customdatapager customdatagrid = d as customdatapager;
if (customdatagrid != null)
{
listlist = new list ;();
int recordcount = (int)e.newvalue;
for (int i = 0; i < recordcount; i++)
{
list.add(i);
}pagedcollectionview pagedcollectionview = new pagedcollectionview(list);
customdatagrid.source = pagedcollectionview;
}
}
然后在自定义的datagrid中调用即可:
private void binddatasource()
{
listdatacollection = new list ();
for (int i = 0; i < 1000; i++)
{
datacollection.add(i.tostring());
}pagedcollectionview pagedcollectionview = new pagedcollectionview(datacollection);
dgresult.itemssource = pagedcollectionview;
dpresult.pagecount = datacollection.count;
}