注:此文原来写的,后来忘了什么原因删除了,在网上看到有人转载,我在转回来。
做项目时需要数据分页在网站找了半天也没有找到合适的,没办法自己写个,项目比较着急先凑合写个吧,有时间在重写下。
页面部分代码
<UserControl x:Class="Test.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">
<StackPanel Orientation="Horizontal">
<Button Name="FirstButton" Content="首页" Margin="5" Foreground="White" Background="Black" Width="55" VerticalAlignment="Center" Click="FirstButton_Click"/>
<Button Name="PreButton" Content="上一页" Margin="5" Foreground="White" Background="Black" Width="55" VerticalAlignment="Center" Click="PreButton_Click"/>
<Button Name="NextButton" Content="下一页" Margin="5" Foreground="White" Background="Black" Width="55" VerticalAlignment="Center" Click="NextButton_Click"/>
<Button Name="LastButton" Content="尾页" Margin="5" Foreground="White" Background="Black" Width="55" VerticalAlignment="Center" Click="LastButton_Click"/>
<TextBlock VerticalAlignment="Center" FontSize="12" >
<TextBlock Text="【共"/>
<TextBlock Name="txtCount" Text="" Foreground="Red"/>
<TextBlock Text="页】"/>
<TextBlock Text="【当前第"/>
<TextBlock Name="txtCurrent" Foreground="Red"/>
<TextBlock Text="页】"/>
<TextBlock Text="【共"/>
<TextBlock Name="txtAll" Foreground="Red"/>
<TextBlock Text="条记录】"/>
</TextBlock>
</StackPanel>
</UserControl>
后台代码部分:
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;
namespace Test
{
public delegate void PagerIndexChangingEvent(int pageIndex, EventArgs e);
/// <summary>
/// Pager.xaml 的交互逻辑
/// </summary>
public partial class Pager : UserControl
{
public Pager()
{
InitializeComponent();
}
public event PagerIndexChangingEvent PageIndexChanging;
/// <summary>
/// 当前页索引
/// </summary>
private int pageIndex = 0;
public int PageIndex
{
get { return pageIndex; }
set { pageIndex = value; }
}
/// <summary>
/// 总页数
/// </summary>
private int pageCount;
/// <summary>
/// 一页个数
/// </summary>
private int pageSize=12;
public int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
//数据个数
private int count;
public int Count
{
get { return count; }
set
{
count = value;
if (count == 0)
pageCount = 0;
else
pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
txtCount.Text = pageCount.ToString();
txtAll.Text = count.ToString();
Init(null);
}
}
void Init(EventArgs e)
{
try
{
InitButton();
int temp = pageIndex + 1;
if (pageCount == 0)
txtCurrent.Text = "0";
else
txtCurrent.Text = temp.ToString();
if (e != null)
{
PageIndexChanging(pageIndex, e);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
void InitButton()
{
this.FirstButton.IsEnabled = true;
this.PreButton.IsEnabled = true;
this.NextButton.IsEnabled = true;
this.LastButton.IsEnabled = true;
this.FirstButton.Background = Brushes.Black;
this.PreButton.Background = Brushes.Black;
this.NextButton.Background = Brushes.Black;
this.LastButton.Background = Brushes.Black;
//总共一页
if (pageCount < 2)
{
this.FirstButton.IsEnabled = false;
this.PreButton.IsEnabled = false;
this.NextButton.IsEnabled = false;
this.LastButton.IsEnabled = false;
this.FirstButton.Background = Brushes.Red;
this.PreButton.Background = Brushes.Red;
this.NextButton.Background = Brushes.Red;
this.LastButton.Background = Brushes.Red;
return;
}
//第一页
if (pageIndex == 0)
{
this.FirstButton.IsEnabled = false;
this.PreButton.IsEnabled = false;
this.FirstButton.Background = Brushes.Red;
this.PreButton.Background = Brushes.Red;
return;
}
//最后一页
if (pageIndex+1 == pageCount)
{
this.NextButton.IsEnabled = false;
this.LastButton.IsEnabled = false;
this.NextButton.Background = Brushes.Red;
this.LastButton.Background = Brushes.Red;
}
}
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FirstButton_Click(object sender, RoutedEventArgs e)
{
pageIndex = 0;
Init(e);
}
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PreButton_Click(object sender, RoutedEventArgs e)
{
--pageIndex;
Init(e);
}
/// <summary>
/// 下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NextButton_Click(object sender, RoutedEventArgs e)
{
++pageIndex;
Init(e);
}
/// <summary>
/// 尾页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LastButton_Click(object sender, RoutedEventArgs e)
{
pageIndex = pageCount - 1;
Init(e);
}
}
}
效果图:
按钮增加了样式~