WPF的简单分页控件

参考链接:https://www.cnblogs.com/scheshan/archive/2012/06/19/2554550.html

整合了一下并修改了自定义分页页数。

控件页面:

<UserControl x:Class="SCADA.UserControls.DataPager"<!-改为自己项目的文件夹->
             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" 
             xmlns:local="clr-namespace:SCADA.UserControls"
             mc:Ignorable="d" 
             x:Name="userControl"
             d:DesignHeight="30" d:DesignWidth="600">
    <UserControl.Resources>
        <Style x:Key="NormalTextBlockStyle" TargetType="{x:Type TextBlock}">
            <Setter Property="TextWrapping" Value="NoWrap"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
        <ControlTemplate x:Key="PageButtonTemplate" TargetType="{x:Type Button}">
            <TextBlock x:Name="textBlock" VerticalAlignment="Center" Text="{TemplateBinding Content}" HorizontalAlignment="Stretch" Cursor="Hand" Foreground="Blue"/>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" TargetName="textBlock" Value="#FFABA5A5"/>
                    <Setter Property="Cursor" Value="None"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </UserControl.Resources>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <TextBlock Style="{DynamicResource NormalTextBlockStyle}" Text="共"/>
        <TextBlock Text="{Binding TotalCount, ElementName=userControl}" Style="{DynamicResource NormalTextBlockStyle}" Margin="4,0"/>
        <TextBlock Style="{DynamicResource NormalTextBlockStyle}" Text="行" />
       
        <ComboBox VerticalAlignment="Center" Margin="5 0 5 0" x:Name="theSize" SelectionChanged="ChangePageSize">
            <ComboBoxItem IsSelected="True">10行/页</ComboBoxItem>
            <ComboBoxItem>30行/页</ComboBoxItem>
            <ComboBoxItem>50行/页</ComboBoxItem>
            <ComboBoxItem>100行/页</ComboBoxItem>
            <ComboBoxItem>500行/页</ComboBoxItem>
            <ComboBoxItem>1000行/页</ComboBoxItem>
            <ComboBoxItem>3000行/页</ComboBoxItem>
            <ComboBoxItem>5000行/页</ComboBoxItem>
        </ComboBox>
        
        <TextBlock Text="第" Style="{DynamicResource NormalTextBlockStyle}"/>
        <TextBlock Text="{Binding PageIndex, ElementName=userControl}" Style="{DynamicResource NormalTextBlockStyle}" Margin="4,0,2,0"/>
        <TextBlock Text="/" Style="{DynamicResource NormalTextBlockStyle}"/>
        <TextBlock Text="{Binding PageCount, ElementName=userControl, Mode=OneWay}" Style="{DynamicResource NormalTextBlockStyle}" Margin="2,0,4,0"/>
        <TextBlock Text="页" Style="{DynamicResource NormalTextBlockStyle}"/>
        <Button x:Name="btnFirst" Content="首页" Template="{DynamicResource PageButtonTemplate}" VerticalAlignment="Center" Margin="10,0,0,0" Click="btnFirst_Click" IsEnabled="{Binding CanGoFirstOrPrev, ElementName=userControl}" />
        <Button x:Name="btnPrev" Content="上一页" Template="{DynamicResource PageButtonTemplate}" VerticalAlignment="Center" Margin="10,0,0,0" Click="btnPrev_Click" IsEnabled="{Binding CanGoFirstOrPrev, ElementName=userControl}" />
        <Button x:Name="btnNext" Content="下一页" VerticalAlignment="Center" Template="{DynamicResource PageButtonTemplate}" Margin="10,0,0,0" Click="btnNext_Click" IsEnabled="{Binding CanGoLastOrNext, ElementName=userControl}" />
        <Button x:Name="btnLast" VerticalAlignment="Center" Template="{DynamicResource PageButtonTemplate}" Margin="10,0,0,0" Click="btnLast_Click" IsEnabled="{Binding CanGoLastOrNext, ElementName=userControl}" Content="末页" />
        <TextBox x:Name="txtPageIndex" Width="40" VerticalAlignment="Center" Margin="10,0,0,0" Text="{Binding PageIndex, ElementName=userControl, Mode=OneWay}"/>
        <Button x:Name="btnGoTo" Content="转到" Template="{DynamicResource PageButtonTemplate}" VerticalAlignment="Center" Margin="10,0,0,0" Click="btnGoTo_Click" />
    </StackPanel>
</UserControl>

页面的cs:

 /// <summary>
 /// DataPager.xaml 的交互逻辑
 /// </summary>
 public partial class DataPager : UserControl, INotifyPropertyChanged
 {
     public DataPager()
     {
         InitializeComponent();
     }
     /// <summary>
     /// 分页前处理的事件,如果设置e.IsCancel=True将取消分页
     /// </summary>
     public event PageChangingRouteEventHandler PageChanging;
     /// <summary>
     /// 分页后处理的事件
     /// </summary>
     public event PageChangedRouteEventHandler PageChanged;

     #region 依赖属性
     /// <summary>
     /// 当前页
     /// </summary>
     public int PageIndex
     {
         get { return (int)GetValue(PageIndexProperty); }
         set { SetValue(PageIndexProperty, value); }
     }

     // Using a DependencyProperty as the backing store for CurrentPage.  This enables animation, styling, binding, etc...
     public static readonly DependencyProperty PageIndexProperty =
         DependencyProperty.Register("PageIndex", typeof(int), typeof(DataPager), new UIPropertyMetadata(1, (sender, e) =>
         {
             var dp = sender as DataPager;
             dp.ChangeNavigationButtonState();
         }));

     /// <summary>
     /// 每页显示数据大小
     /// </summary>
     public int PageSize
     {
         get { return (int)GetValue(PageSizeProperty); }
         set { SetValue(PageSizeProperty, value); InitData(); }
     }

     // Using a DependencyProperty as the backing store for PageSize.  This enables animation, styling, binding, etc...
     public static readonly DependencyProperty PageSizeProperty =
         DependencyProperty.Register("PageSize", typeof(int), typeof(DataPager), new UIPropertyMetadata(10, (sender, e) =>
         {
             var dp = sender as DataPager;
             if (dp == null) return;
             dp.ChangeNavigationButtonState();
         }));


     /// <summary>
     /// 记录数量
     /// </summary>
     public int TotalCount
     {
         get { return (int)GetValue(TotalCountProperty); }
         set
         {
             SetValue(TotalCountProperty, value);
         }
     }

     // Using a DependencyProperty as the backing store for TotalCount.  This enables animation, styling, binding, etc...
     public static readonly DependencyProperty TotalCountProperty =
         DependencyProperty.Register("TotalCount", typeof(int), typeof(DataPager), new UIPropertyMetadata(0, (sender, e) =>
         {
             var dp = sender as DataPager;
             if (dp == null) return;
             dp.InitData();
             dp.ChangeNavigationButtonState();
         }));


     /// <summary>
     /// 总页数
     /// </summary>
     public int PageCount
     {
         get { return (int)GetValue(PageCountProperty); }
         private set { SetValue(PageCountProperty, value); }
     }

     // Using a DependencyProperty as the backing store for PageCount.  This enables animation, styling, binding, etc...
     public static readonly DependencyProperty PageCountProperty =
         DependencyProperty.Register("PageCount", typeof(int), typeof(DataPager), new UIPropertyMetadata(1));
     /// <summary>
     /// 是否可以点击首页和上一页按钮
     /// </summary>
     public bool CanGoFirstOrPrev
     {
         get
         {
             if (PageIndex <= 1) return false;
             return true;
         }
     }
     /// <summary>
     /// 是否可以点击最后页和下一页按钮
     /// </summary>
     public bool CanGoLastOrNext
     {
         get
         {
             if (PageIndex >= PageCount) return false;
             return true;
         }
     }
     #endregion
     /// <summary>
     /// 点击首页按钮
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     private void btnFirst_Click(object sender, RoutedEventArgs e)
     {
         OnPageChanging(1);
     }
     /// <summary>
     /// 点击上一页按钮
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     private void btnPrev_Click(object sender, RoutedEventArgs e)
     {
         OnPageChanging(this.PageIndex - 1);
     }
     /// <summary>
     /// 点击下一页按钮
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     private void btnNext_Click(object sender, RoutedEventArgs e)
     {
         OnPageChanging(this.PageIndex + 1);
     }
     /// <summary>
     /// 点击末页按钮
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     private void btnLast_Click(object sender, RoutedEventArgs e)
     {
         OnPageChanging(this.PageCount);
     }
     /// <summary>
     /// 点击跳转按钮
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     private void btnGoTo_Click(object sender, RoutedEventArgs e)
     {
         int pageIndex = 1;
         try
         {
             pageIndex = Convert.ToInt32(txtPageIndex.Text);
         }
         catch
         {

         }
         finally
         {
             OnPageChanging(pageIndex);
         }
     }
     /// <summary>
     /// 选择页码事件
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     private void ChangePageSize(object sender, SelectionChangedEventArgs e)
     {
         if (theSize.SelectedItem != null)
         {
             var Content = ((System.Windows.Controls.ComboBoxItem)theSize.SelectedItem).Content;
             switch (Content)
             {
                 case null: PageSize = 10; break;
                 case "10行/页": PageSize = 10; break;
                 case "30行/页": PageSize = 30; break;
                 case "50行/页": PageSize = 50; break;
                 case "100行/页": PageSize = 100; break;
                 case "500行/页": PageSize = 500; break;
                 case "1000行/页": PageSize = 1000; break;
                 case "3000行/页": PageSize = 3000; break;
                 case "5000行/页": PageSize = 5000; break;
             }
                OnPageChanging(PageIndex);
         }
     }

     /// <summary>
     /// 页码更改
     /// </summary>
     /// <param name="pageIndex"></param>
     internal void OnPageChanging(int pageIndex)
     {
         if (pageIndex < 1) pageIndex = 1;
         if (pageIndex > this.PageCount) pageIndex = this.PageCount;

         var oldPageIndex = this.PageIndex;
         var newPageIndex = pageIndex;
         var eventArgs = new PageChangingEventArgs() { OldPageIndex = oldPageIndex, NewPageIndex = newPageIndex, CurrentPageSize = PageSize };
         if (this.PageChanging != null)
         {
             this.PageChanging(this, eventArgs);
         }
         if (!eventArgs.IsCancel)
         {
             this.PageIndex = newPageIndex;
             if (this.PageChanged != null)
             {
                 this.PageChanged.Invoke(this, new PageChangedEventArgs() { CurrentPageIndex = this.PageIndex, CurrentPageSize = PageSize });
             }
         }
     }
     /// <summary>
     /// 通知导航按钮(首页,上一页,下一页,末页)状态的更改
     /// </summary>
     void ChangeNavigationButtonState()
     {
         this.NotifyPropertyChanged("CanGoFirstOrPrev");
         this.NotifyPropertyChanged("CanGoLastOrNext");
     }
     /// <summary>
     /// 初始化数据
     /// </summary>
     void InitData()
     {
         if (this.PageSize < 1)
         {
             this.PageSize = 10;
         }
         if (this.TotalCount == 0)
         {
             this.PageCount = 1;
         }
         else
         {
             this.PageCount = this.TotalCount % this.PageSize > 0 ? (this.TotalCount / this.PageSize) + 1 : this.TotalCount / this.PageSize;
         }
         if (this.PageIndex < 1)
         {
             this.PageIndex = 1;
         }
         if (this.PageIndex > this.PageCount)
         {
             this.PageIndex = this.PageCount;
         }

     }


     #region INotifyPropertyChanged成员
     public event PropertyChangedEventHandler PropertyChanged;
     public void NotifyPropertyChanged(string propertyName)
     {
         if (this.PropertyChanged != null)
         {
             this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
         }
     }
     #endregion

 }
 public class PageChangedEventArgs : RoutedEventArgs
 {
     /// <summary>
     /// 当前页码
     /// </summary>
     public int CurrentPageIndex { get; set; }
     /// <summary>
     /// 当前分页大小
     /// </summary>
     public int CurrentPageSize { get; set; }
 }
 public class PageChangingEventArgs : RoutedEventArgs
 {
     /// <summary>
     /// 页码更新前的页码
     /// </summary>
     public int OldPageIndex { get; set; }
     /// <summary>
     /// 当前页码
     /// </summary>
     public int NewPageIndex { get; set; }
     /// <summary>
     /// 当前分页大小
     /// </summary>
     public int CurrentPageSize { get; set; }
     public bool IsCancel { get; set; }
 }
 public delegate void PageChangingRouteEventHandler(object sender, PageChangingEventArgs e);
 public delegate void PageChangedRouteEventHandler(object sender, PageChangedEventArgs e);

在DataGraid页面引用:

<!-local是在xmnl引用的本地路径xmlns:local="clr-namespace:***"->
<local:DataPager PageChanged="ChangedPage" PageChanging="ChangingPage" TotalCount="绑定后台数据源的总数"  PageIndex="绑定后台数据源的当前页"/>

在后台的ChangedPage与ChangingPage方法在页面改变时都会触发用于跟数据源联动。

/// <summary>
/// 数据分页处理后的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChangedPage(object sender, PageChangedEventArgs e)
{
    var a = e.CurrentPageIndex;
    var c = e.CurrentPageSize;
}
/// <summary>
/// 数据分页处理前的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChangingPage(object sender, PageChangingEventArgs e)
{
    var a = e.NewPageIndex;
    var b=e.OldPageIndex;
    var c=e.CurrentPageSize;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值