效果图:
1、xaml
<UserControl x:Class="app.component.Pager" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" Loaded="UserControl_Loaded"> <Grid> <StackPanel Orientation="Horizontal"> <TextBlock Height="20" Margin="5"> <TextBlock Text="当前" /> <TextBlock Name="currentCountTbk" Foreground="Red" /> <TextBlock Text="条记录,共" /> <TextBlock Name="totalCountTbk" Foreground="Red" /> <TextBlock Text="条 " /> <TextBlock Text="第" /> <TextBlock Name="pageNoTbk" Foreground="Red" /> <TextBlock Text="/" /> <TextBlock Name="pageCountTbk" Foreground="Red" /> <TextBlock Text="页 " /> </TextBlock> <TextBlock Text="每页显示" Margin="5"/> <TextBox Name="pageSizeTb" Text="" Width="25" Height="20" /> <Button Content="设置" Click="setPageSizeBtn_Click" Height="22"/> <TextBlock Text=" " /> <Button Name="firstPageBtn" Content="首页" VerticalAlignment="Center" Click="firstPageBtn_Click"/> <Button Name="prePageBtn" Content="上一页" VerticalAlignment="Center" Click="prePageBtn_Click"/> <Button Name="nextPageBtn" Content="下一页" VerticalAlignment="Center" Click="nextPageBtn_Click"/> <Button Name="lastPageBtn" Content="末页" VerticalAlignment="Center" Click="lastPageBtn_Click"/> <TextBlock Text=" 转到" Margin="5"/> <TextBox Name="gotoPageNoTb" Text="" Width="25" Height="20" /> <TextBlock Text="页" Margin="5"/> <Button Content=" GO " Click="gotoBtn_Click" Height="22"/> <TextBlock Text=" " /> <Button Content="刷新" Click="refreshBtn_Click" Height="22"/> </StackPanel> </Grid> </UserControl>
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; using System.Text.RegularExpressions; using System.Data; using app.component; namespace app.component { /// <summary> /// Pager.xaml 的交互逻辑 /// </summary> public partial class Pager : UserControl { private int pageNo = 1; // 当前页 private int pageSize = 5; // 每页记录数 private int totalCount = 0; // 总记录数 private int currentCount = 0; // 当前页记录数 private int pageCount = 1; // 总页数 private bool gotoFirstPageAfterLoaded = true; // 控件初始化后是否自动加载第一页数据 private bool hasInit = false; public Pager() { InitializeComponent(); this.prePageBtn.IsEnabled = false; } /// <summary> /// 获取数据委托,返回总记录数 /// </summary> /// <param name="pageNo">请求页</param> /// <param name="pageSize">每页记录数</param> /// <returns>总记录数</returns> public delegate int GetDataDelegate(int pageNo, int pageSize); private GetDataDelegate getDataDelegateHandler; /// <summary> /// 刷新当前页 /// </summary> public void Refresh() { GotoPage(pageNo); } public void GotoFirstPage() { GotoPage(1); } public void GotoLastPage() { GotoPage(pageCount); } public void GotoPage(int pageNo) { if (pageNo <= 0) { pageNo = 1; } this.pageNo = pageNo; try { totalCount = getDataDelegateHandler(pageNo, pageSize); pageCount = totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize + 1); currentCount = pageNo == pageCount ? (totalCount - (pageNo - 1) * pageSize) : pageSize; // 页码显示 this.currentCountTbk.Text = currentCount + ""; this.totalCountTbk.Text = totalCount + ""; this.pageNoTbk.Text = pageNo + ""; this.pageCountTbk.Text = pageCount + ""; this.pageSizeTb.Text = pageSize + ""; // 按钮状态 this.prePageBtn.IsEnabled = pageNo > 1 ? true : false; this.firstPageBtn.IsEnabled = pageNo > 1 ? true : false; this.nextPageBtn.IsEnabled = pageNo < pageCount ? true : false; this.lastPageBtn.IsEnabled = pageNo < pageCount ? true : false; } catch (Exception) { this.pageNoTbk.Text = ""; this.pageCountTbk.Text = ""; } } // 设置页显示记录数 private void setPageSizeBtn_Click(object sender, RoutedEventArgs e) { try { int pageSize = Convert.ToInt32(this.pageSizeTb.Text); if (pageSize > 0) { this.pageSize = pageSize; this.GotoFirstPage(); } else { this.pageSizeTb.Text = this.pageSize + ""; } } catch (Exception) { this.pageSizeTb.Text = this.pageSize + ""; } } // 首页事件 private void firstPageBtn_Click(object sender, RoutedEventArgs e) { GotoFirstPage(); } // 上一页事件 private void prePageBtn_Click(object sender, RoutedEventArgs e) { if (pageNo > 1) { pageNo -= 1; GotoPage(pageNo); } } // 下一页事件 private void nextPageBtn_Click(object sender, RoutedEventArgs e) { if (pageNo == 1 || pageNo < pageCount) { pageNo += 1; GotoPage(pageNo); } } // 末页事件 private void lastPageBtn_Click(object sender, RoutedEventArgs e) { GotoLastPage(); } // 跳转事件 private void gotoBtn_Click(object sender, RoutedEventArgs e) { try { int pageNo = Convert.ToInt32(this.gotoPageNoTb.Text); if (pageNo >= 1 && pageNo <= pageCount) { GotoPage(pageNo); } else { MessageBox.Show("请输入正确的页码范围:1 ~ " + pageCount); } } catch (Exception) { } } // 刷新 private void refreshBtn_Click(object sender, RoutedEventArgs e) { Refresh(); } private void UserControl_Loaded(object sender, RoutedEventArgs e) { if (!hasInit) { if (gotoFirstPageAfterLoaded) { GotoPage(1); } hasInit = true; } } // getter setter public int PageSize { get { return pageSize; } set { if (value > 0) { pageSize = value; } } } /// <summary> /// 控件初始化后是否自动加载第一页数据 /// </summary> public bool GotoFirstPageAfterLoaded { get { return gotoFirstPageAfterLoaded; } set { gotoFirstPageAfterLoaded = value; } } public Pager.GetDataDelegate GetDataDelegateHandler { set { getDataDelegateHandler = value; } } } }
3、调用示例
xmlns:app="clr-namespace:app.component"
//用DataGrid装载数据
<DockPanel> <app:Pager x:Name="pager" PageSize="5" GetDataDelegateHandler="LoadData" DockPanel.Dock="Top" /> <DataGrid Name="dg" IsReadOnly="True" DockPanel.Dock="Bottom" LoadingRow="dg_LoadingRow"></DataGrid> </DockPanel>
后台:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } /// <summary> /// 分页控件回调函数,返回总记录数 /// </summary> /// <param name="pageNo">页码,由分页控件传入</param> /// <param name="pageSize">页面记录大小,由分页控件传入</param> /// <returns></returns> private int LoadData(int pageNo, int pageSize) { // Page<Dic> page = this.dicInfoService.GetDicList(pageNo, pageSize, "asc", "id", new Dic()); ObservableCollection<Person> items = new ObservableCollection<Person>();
//初始化数据 Random rnd=new Random(); for (int i = 0; i < 30; i++) { items.Add(new Person { Name = "张三"+rnd.Next(50), age=rnd.Next(100).ToString(), birthday = "2013-3-6", remark = "说明" }); } //绑定数据 this.dg.ItemsSource = items.Skip((pageNo-1)*pageSize).Take(pageSize).ToList(); return items.Count; } private void dg_LoadingRow(object sender, DataGridRowEventArgs e) { e.Row.Header= e.Row.GetIndex() + 1; } } public class Person { public string Name { get; set; } public string age{ get; set; } public string birthday { get; set; } public string remark { get; set; } }