在Silverlight中DataGrid分页可以结合DataPager控件很容易实现,但是在WPF中没有类似的,需要手动实现这样一个控件:
1、创建一个UserControl,DP.xaml,代码如下,可以直接拷贝使用:
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.Data;
using System.Text.RegularExpressions;
namespace WFPSys.UserControls
{
///
/// DP.xaml 的交互逻辑
///
public partial class DP : UserControl
{
public DP()
{
InitializeComponent();
this.Loaded += delegate
{
//首页
this.btnFirst.MouseLeftButtonUp += new MouseButtonEventHandler(btnFirst_Click);
this.btnFirst.MouseLeftButtonDown += new MouseButtonEventHandler(btnFirst_MouseLeftButtonDown);
//上一页
this.btnPrev.MouseLeftButtonUp += new MouseButtonEventHandler(btnPrev_Click);
this.btnPrev.MouseLeftButtonDown += new MouseButtonEventHandler(btnPrev_MouseLeftButtonDown);
//下一页
this.btnNext.MouseLeftButtonUp += new MouseButtonEventHandler(btnNext_Click);
this.btnNext.MouseLeftButtonDown += new MouseButtonEventHandler(btnNext_MouseLeftButtonDown);
//末页
this.btnLast.MouseLeftButtonUp += new MouseButtonEventHandler(btnLast_Click);
this.btnLast.MouseLeftButtonDown += new MouseButtonEventHandler(btnLast_MouseLeftButtonDown);
this.btnGo.Click += new RoutedEventHandler(btnGo_Click);
};
}
private DataTable _dt = new DataTable();
//每页显示多少条
private int pageNum = 10;
//当前是第几页
private int pIndex = 1;
//对象
private DataGrid grdList;
//最大页数
private int MaxIndex = 1;
//一共多少条
private int allNum = 0;
#region 初始化数据
///
/// 初始化数据
///
///
///
///
public void ShowPages(DataGrid grd, DataTable ds, int Num)
{
if (ds == null || ds.Rows.Count == 0)
return;
if (ds.Rows.Count == 0)
return;
DataTable dt = ds;
this._dt = dt.Clone();
this.grdList = grd;
this.pageNum = Num;
this.pIndex = 1;
foreach (DataRow r in dt.Rows)
this._dt.ImportRow(r);
SetMaxIndex();
ReadDataTable();
if (this.MaxIndex > 1)
{
this.pageGo.IsReadOnly = false;
this.btnGo.IsEnabled = true;
}
}
#endregion
#region 画数据
///
/// 画数据
///
private void ReadDataTable()
{
try
{
DataTable tmpTable = new DataTable();
tmpTable = this._dt.Clone();
int first = this.pageNum * (this.pIndex - 1);
first = (first > 0) ? first : 0;
//如果总数量大于每页显示数量
if (this._dt.Rows.Count >= this.pageNum * this.pIndex)
{
for (int i = first; i < pageNum * this.pIndex; i++)
tmpTable.ImportRow(this._dt.Rows[i]);
}
else
{
for (int i = first; i < this._dt.Rows.Count; i++)
tmpTable.ImportRow(this._dt.Rows[i]);
}
this.grdList.ItemsSource = tmpTable.DefaultView;
tmpTable.Dispose();
}
catch
{
MessageBox.Show("错误");
}
finally
{
DisplayPagingInfo();
}
}
#endregion
#region 画每页显示等数据
///
/// 画每页显示等数据
///
private void DisplayPagingInfo()
{
if (this.pIndex == 1)
{
this.btnPrev.IsEnabled = false;
this.btnFirst.IsEnabled = false;
}
else
{
this.btnPrev.IsEnabled = true;
this.btnFirst.IsEnabled = true;
}
if (this.pIndex == this.MaxIndex)
{
this.btnNext.IsEnabled = false;
this.btnLast.IsEnabled = false;
}
else
{
this.btnNext.IsEnabled = true;
this.btnLast.IsEnabled = true;
}
this.tbkRecords.Text = string.Format("每页{0}条/共{1}条", this.pageNum, this.allNum);
int first = (this.pIndex - 4) > 0 ? (this.pIndex - 4) : 1;
int last = (first + 9) > this.MaxIndex ? this.MaxIndex : (first + 9);
this.grid.Children.Clear();
for (int i = first; i <= last; i++)
{
ColumnDefinition cdf = new ColumnDefinition();
this.grid.ColumnDefinitions.Add(cdf);
TextBlock tbl = new TextBlock();
tbl.Text = i.ToString();
tbl.Style = FindResource("PageTextBlock3") as Style;
tbl.MouseLeftButtonUp += new MouseButtonEventHandler(tbl_MouseLeftButtonUp);
tbl.MouseLeftButtonDown += new MouseButtonEventHandler(tbl_MouseLeftButtonDown);
if (i == this.pIndex)
tbl.IsEnabled = false;
Grid.SetColumn(tbl, this.grid.ColumnDefinitions.Count - 1);
Grid.SetRow(tbl, 0);
this.grid.Children.Add(tbl);
}
}
#endregion
#region 首页
///
/// 首页
///
///
///
private void btnFirst_Click(object sender, System.EventArgs e)
{
this.pIndex = 1;
ReadDataTable();
}
///
/// 首页
///
///
///
private void btnFirst_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
#endregion
#region 上一页
///
/// 上一页
///
///
///
private void btnPrev_Click(object sender, System.EventArgs e)
{
if (this.pIndex <= 1)
return;
this.pIndex--;
ReadDataTable();
}
///
/// 上一页
///
///
///
private void btnPrev_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
#endregion
#region 下一页
///
/// 下一页
///
///
///
private void btnNext_Click(object sender, System.EventArgs e)
{
if (this.pIndex >= this.MaxIndex)
return;
this.pIndex++;
ReadDataTable();
}
///
/// 下一页
///
///
///
private void btnNext_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
#endregion
#region 未页
///
/// 未页
///
///
///
private void btnLast_Click(object sender, System.EventArgs e)
{
this.pIndex = this.MaxIndex;
ReadDataTable();
}
///
/// 未页
///
///
///
private void btnLast_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
#endregion
#region 设置最多大页面
///
/// 设置最多大页面
///
private void SetMaxIndex()
{
//多少页
int Pages = this._dt.Rows.Count / pageNum;
if (this._dt.Rows.Count != (Pages * pageNum))
{
if (_dt.Rows.Count < (Pages * pageNum))
Pages--;
else
Pages++;
}
this.MaxIndex = Pages;
this.allNum = this._dt.Rows.Count;
}
#endregion
#region 跳转到多少页
///
/// 跳转到多少页
///
///
///
private void btnGo_Click(object sender, RoutedEventArgs e)
{
if (IsNumber(this.pageGo.Text))
{
int pageNum = int.Parse(this.pageGo.Text);
if (pageNum > 0 && pageNum <= this.MaxIndex)
{
this.pIndex = pageNum;
ReadDataTable();
}
else if (pageNum > this.MaxIndex)
{
this.pIndex = this.MaxIndex;
ReadDataTable();
}
}
this.pageGo.Text = "";
}
#endregion
#region 分页数字的点击触发事件
private void tbl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
TextBlock tbl = sender as TextBlock;
if (tbl == null)
return;
int index = int.Parse(tbl.Text.ToString());
this.pIndex = index;
if (index > this.MaxIndex)
this.pIndex = this.MaxIndex;
if (index < 1)
this.pIndex = 1;
ReadDataTable();
}
void tbl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
#endregion
private static Regex RegNumber = new Regex("^[0-9]+$");
#region 判断是否是数字
///
/// 判断是否是数字
///
///
///
public static bool IsNumber(string valString)
{
Match m = RegNumber.Match(valString);
return m.Success;
}
#endregion
}
}
在WPF窗体中添加该用户控件,如下:
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.Shapes;
using System.Collections.ObjectModel;
using Syit.Models;
using System.Data;
using Syit.BLLs;
namespace WFPSys.JCZL
{
///
/// BigClass.xaml 的交互逻辑
///
public partial class BigClass : UserControl
{
public BigClass()
{
InitializeComponent();
}
#region 功能按钮事件
///
/// 添加数据
///
///
///
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
}
///
/// 刷新数据
///
///
///
private void btnReload_Click(object sender, RoutedEventArgs e)
{
}
///
/// 删除收支项目数据
///
///
///
private void mitmDelete_Click(object sender, RoutedEventArgs e)
{
if (true)
{
MessageBoxResult boxResult = MessageBox.Show(string.Format("您确定要删除数据【{0}】所包含的信息吗?", ""), "询问:", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (boxResult == MessageBoxResult.Yes)
{
MessageBox.Show("数据删除成功!", "提示:", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
}
}
}
#endregion
#region 页面加载事件
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
tb_BigTypesBLL bll = new tb_BigTypesBLL();
ObservableCollection list = new ObservableCollection();
foreach (DataRowView drv in bll.GetData().DefaultView)
{
tb_BigTypes item = new tb_BigTypes();
item.ID = int.Parse(drv["ID"].ToString());
item.TypeName = drv["TypeName"].ToString();
item.Description = drv["Description"].ToString();
item.IsDelete = int.Parse(drv["IsDelete"].ToString());
list.Add(item);
}
this.dgDataSource.DataContext = list; // 为DataGrid绑定数据源
this.page.ShowPages(this.dgDataSource, bll.GetData(), 5); //这里是调用设置分页的函数
}
#endregion
}
}