您的位置:首页 > 其它

一个简单WPF分页条控件

2016-05-15 16:16 204 查看
分享一个自己在用的简单WPF分页条控件。

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"
<app:Pager x:Name="pager" PageSize="5" GetDataDelegateHandler="LoadData" />

/// <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<Dic> items = new ObservableCollection<Dic>();
foreach (Dic item in page.ResultList) {
items.Add(item);
}

this.dataGrid.DataContext = items;

return page.Total;
}

4、效果图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  WPF分页控件