发布一个wpf的分页控件.配合存储过程实现分页.

 

先看看图:

 

前台代码:

 

< UserControl x:Class = " Layout.UI.Comm.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 "  
>
    
< Grid >
        
< Label Content = ""  Height = " 28 "  HorizontalAlignment = " Left "  Margin = " 0,0,0,0 "  Name = " lblinfo "  VerticalAlignment = " Center "   />
        
< Button Content = " 上一页 "  Height = " 23 "  HorizontalAlignment = " Left "  Margin = " 172,0,0,0 "  Name = " btnPrev "  VerticalAlignment = " Center "  Width = " 75 "  Click = " btnPrev_Click "   />
        
< Button Content = " 下一页 "  Height = " 23 "  HorizontalAlignment = " Right "  Margin = " 0,0,272,0 "  Name = " btnNext "  VerticalAlignment = " Center "  Width = " 75 "   Click = " btnNext_Click " />
        
< TextBox Height = " 23 "  Margin = " 533,0,0,0 "  Name = " txtCurrentPage "  VerticalAlignment = " Center "  HorizontalAlignment = " Left "  Width = " 34 "   />
        
< Button Content = " 转到 "  HorizontalAlignment = " Right "  Margin = " 0,0,96,0 "  Name = " btnGo "  Width = " 75 "  Height = " 23 "  Click = " btnGo_Click "   />
        
< Label Content = " "   HorizontalAlignment = " Left "  Margin = " 573,0,0,0 "  Name = " label2 "  VerticalAlignment = " Center "   />
        
< Label Content = " "   Margin = " 0,0,68,0 "  Name = " label3 "  VerticalAlignment = " Center "  HorizontalAlignment = " Right "    />
        
< Button Content = " 首页 "  Height = " 23 "  HorizontalAlignment = " Left "  Margin = " 91,0,0,0 "  Name = " btnFirst "  VerticalAlignment = " Center "  Width = " 75 "  Click = " btnFirst_Click "   />
        
< Button Content = " 末页 "  Height = " 23 "  HorizontalAlignment = " Left "  Margin = " 334,0,0,0 "  Name = " btnLast "  VerticalAlignment = " Center "  Width = " 75 "  Click = " btnLast_Click "   />        
    
</ Grid >
</ 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  Layout.UI.Comm
{
    
///   <summary>
    
///  申明委托
    
///   </summary>
    
///   <param name="e"></param>
    
///   <returns></returns>
     public   delegate   int  EventPagingHandler(EventPagingArg e);
    
///   <summary>
    
/// wpf分页控件.钟健2011年4月7日16:38:06
    
///   </summary>
    
///  
     public   partial   class  Pager : UserControl
    {
        
public  Pager()
        {
            InitializeComponent();
        }

        
public   event  EventPagingHandler EventPaging;
        
///   <summary>
        
///  每页显示记录数
        
///   </summary>
         private   int  _pageSize  =   20 ;
        
///   <summary>
        
///  每页显示记录数
        
///   </summary>
         public   int  PageSize
        {
            
get  {  return  _pageSize; }
            
set
            {
                _pageSize 
=  value;
                GetPageCount();
            }
        }

        
private   int  _nMax  =   0 ;
        
///   <summary>
        
///  总记录数
        
///   </summary>
         public   int  NMax
        {
            
get  {  return  _nMax; }
            
set
            {
                _nMax 
=  value;
                GetPageCount();
            }
        }

        
private   int  _pageCount  =   0 ;
        
///   <summary>
        
///  页数=总记录数/每页显示记录数
        
///   </summary>
         public   int  PageCount
        {
            
get  {  return  _pageCount; }
            
set  { _pageCount  =  value; }
        }

        
private   int  _pageCurrent  =   0 ;
        
///   <summary>
        
///  当前页号
        
///   </summary>
         public   int  PageCurrent
        {
            
get  {  return  _pageCurrent; }
            
set  { _pageCurrent  =  value; }
        }

       

        
private   void  GetPageCount()
        {
            
if  ( this .NMax  >   0 )
            {
                
this .PageCount  =  Convert.ToInt32(Math.Ceiling(Convert.ToDouble( this .NMax)  /  Convert.ToDouble( this .PageSize)));
            }
            
else
            {
                
this .PageCount  =   0 ;
            }
        }

        
///   <summary>
        
///  翻页控件数据绑定的方法
        
///   </summary>
         public   void  Bind()
        {
            
if  ( this .EventPaging  !=   null )
            {
                
this .NMax  =   this .EventPaging( new  EventPagingArg( this .PageCurrent));
            }

            
if  ( this .PageCurrent  >   this .PageCount)
            {
                
this .PageCurrent  =   this .PageCount;
            }
            
if  ( this .PageCount  ==   1 )
            {
                
this .PageCurrent  =   1 ;
            }
            lblinfo.Content 
=   "" + NMax + " 条  "   +   this .PageCurrent.ToString()  +   " / "   +   this .PageCount.ToString() + "" ;
        
            
this .txtCurrentPage.Text  =   this .PageCurrent.ToString();

            
if  ( this .PageCurrent  ==   1 )
            {
                
this .btnPrev.IsEnabled  =   false ;
                
this .btnFirst.IsEnabled  =   false ;
            }
            
else
            {
                btnPrev.IsEnabled 
=   true ;
                btnFirst.IsEnabled 
=   true ;
            }

            
if  ( this .PageCurrent  ==   this .PageCount)
            {
                
this .btnLast.IsEnabled  =   false ;
                
this .btnNext.IsEnabled  =   false ;
            }
            
else
            {
                btnLast.IsEnabled 
=   true ;
                btnNext.IsEnabled 
=   true ;
            }

            
if  ( this .NMax  ==   0 )
            {
                btnNext.IsEnabled 
=   false ;
                btnLast.IsEnabled 
=   false ;
                btnFirst.IsEnabled 
=   false ;
                btnPrev.IsEnabled 
=   false ;
            }
        }

  

        
private   void  btnLast_Click( object  sender, RoutedEventArgs e)
        {
            PageCurrent 
=  PageCount;
            
this .Bind();
        }
      

       

        
private   void  btnNext_Click( object  sender, RoutedEventArgs e)
        {
            
this .PageCurrent  +=   1 ;
            
if  (PageCurrent  >  PageCount)
            {
                PageCurrent 
=  PageCount;
            }
            
this .Bind();
        }

        
private   void  btnGo_Click( object  sender, RoutedEventArgs e)
        {
            
if  ( this .txtCurrentPage.Text  !=   null   &&  txtCurrentPage.Text  !=   "" )
            {
                
if  (Int32.TryParse(txtCurrentPage.Text,  out  _pageCurrent))
                {
                    
this .Bind();
                }
                
else
                {
                    MessageBox.Show(
" 输入数字格式错误! " );
                }
            }
        }

        
private   void  btnFirst_Click( object  sender, RoutedEventArgs e)
        {
            PageCurrent 
=   1 ;
            
this .Bind();
        }

      

        
private   void  btnPrev_Click( object  sender, RoutedEventArgs e)
        {
            PageCurrent 
-=   1 ;
            
if  (PageCurrent  <=   0 )
            {
                PageCurrent 
=   1 ;
            }
            
this .Bind();
        }

    }
    
///   <summary>
    
///  自定义事件数据基类
    
///   </summary>
     public   class  EventPagingArg : EventArgs
    {
        
private   int  _intPageIndex;
        
public  EventPagingArg( int  PageIndex)
        {
            _intPageIndex 
=  PageIndex;
        }
    }
}

 

使用方法:

 

    private   void  Form_Loaded( object  sender, RoutedEventArgs e)
        {
            pager.PageSize 
=   12 ;
            pager.PageCurrent 
=   1 ;
            BindData();
            pager.NMax 
=  total;
        }


 
string  strWhere  =   " IsPass=1 " ;
        
int  total  =   0 ;
        DataSet ds;
        
private   void  BindData()
        {
            ds 
=  OrderRecords.instance.GetList(pager.PageSize, pager.PageCurrent, strWhere,  " Status asc,CurTime Desc " out  total);
            gvOrderList.ItemsSource 
=  ds.Tables[ 0 ].DefaultView;
            gvOrderList.CanUserAddRows 
=   false ;
        }

        
private   int  pager_EventPaging(Comm.EventPagingArg e)
        {
            
int  pagd  =  pager.PageCurrent;
            BindData();
            
return  total;
        }

 

转载于:https://www.cnblogs.com/dukey/archive/2011/08/09/2132187.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF(Windows Presentation Foundation)是一种用于构建Windows客户端应用程序的框架,它提供了丰富的用户界面和数据绑定功能。DataGrid是WPF中的一个强大控件,可用于显示和编辑大量数据。 在WPF中,DataGrid默认不支持分页功能。但是可以通过自定义分页控件实现这个功能。以下是一种实现WPF DataGrid分页控件的方法。 首先,我们可以创建一个自定义的UserControl来实现分页功能。这个UserControl可以包含一个DataGrid和一些用于翻页的按钮(如上一页、下一页、跳转到第一页和最后一页等)。 在UserControl中,我们可以定义一个依赖属性来绑定DataGrid的ItemsSource属性。这样,当我们在使用该分页控件时,只需要将数据源绑定到这个依赖属性即可。 接下来,在代码中,我们可以通过计算每页显示的数据数量,计算总页数,并根据当前页数和每页数据数量来筛选出相应的数据,然后将筛选后的数据绑定到DataGrid的ItemsSource属性上。 同时,我们可以为翻页按钮添加事件处理程序,以便在点击时切换到相应的页数,并更新DataGrid的显示内容。 最后,我们可以在XAML界面中使用这个分页控件,设置DataGrid的样式和布局,并绑定数据源到分页控件的依赖属性上。 总之,通过以上的步骤,我们可以实现一个自定义的WPF DataGrid分页控件,这个控件可以帮助我们在显示大量数据时进行分页,提升用户体验和程序性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值