WPF DataPager控件

最近在项目中遇到远程加载数据的问题,由于服务器采用分页方式返回数据,因此客户端也相应的制作了一个分页控件.代码相对简单,算做入门级的源码.

效果如图:

初步分析,分页功能只需要3个核心变量:PageIndex,PageSize,TotalCount,2个事件:PageChanging,PageChanged,1个方法InitData.

PageIndex:记录当前所在页

PageSize:记录每页显示的条目数

TotalCount:条目总数

由TotalCount和PageSize可以得到PageCount

PageChanging事件作为分页的预处理事件,修改事件参数PageChangingEventArgs的IsCancel属性可以取消分页,这个是参考其他分页控件的属性

PageChanged事件是分页后的处理事件,应用程序可以在此时获取PageIndex进行操作.

InitData方法在数据加载时调用(主要是TotalCount属性),用于初始化上面提到的核心变量.

WPF提供了很强大和实用的Binding功能,在开发控件时,应该尽量把属性设计成依赖属性.因此我把PageIndex,PageSize,TotalCount属性全部设计成依赖属性,并注册了部分回调方法.这样也可以很方便的实现控件和ViewModel的绑定.

核心代码如下:

///

    /// DataPager.xaml 的交互逻辑

    ///

    public partial class DataPager : UserControl, INotifyPropertyChanged

    {

        public DataPager()

        {

            InitializeComponent();

        }

        ///

        /// 分页前处理的事件,如果设置e.IsCancel=True将取消分页

        ///

        public event PageChangingRouteEventHandler PageChanging;

        ///

        /// 分页后处理的事件

        ///

        public event PageChangedRouteEventHandler PageChanged;

        #region 依赖属性

        ///

        /// 当前页

        ///

        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();

            }));

        ///

        /// 每页显示数据大小

        ///

        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(20, (sender, e) =>

            {

                var dp = sender as DataPager;

                if (dp == nullreturn;

                dp.ChangeNavigationButtonState();

            }));

        ///

        /// 记录数量

        ///

        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 == nullreturn;

                dp.InitData();

                dp.ChangeNavigationButtonState();

            }));

        ///

        /// 总页数

        ///

        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));

        

        ///

        /// 是否可以点击首页和上一页按钮

        ///

        public bool CanGoFirstOrPrev

        {

            get

            {

                if (PageIndex <= 1) return false;

                return true;

            }

        }

        ///

        /// 是否可以点击最后页和下一页按钮

        ///

        public bool CanGoLastOrNext

        {

            get

            {

                if (PageIndex >= PageCount) return false;

                return true;

            }

        }

        #endregion

        ///

        /// 点击首页按钮

        ///

        ///

        ///

        private void btnFirst_Click(object sender, RoutedEventArgs e)

        {

            OnPageChanging(1);

        }

        ///

        /// 点击上一页按钮

        ///

        ///

        ///

        private void btnPrev_Click(object sender, RoutedEventArgs e)

        {

            OnPageChanging(this.PageIndex - 1);

        }

        ///

        /// 点击下一页按钮

        ///

        ///

        ///

        private void btnNext_Click(object sender, RoutedEventArgs e)

        {

            OnPageChanging(this.PageIndex + 1);

        }

        ///

        /// 点击末页按钮

        ///

        ///

        ///

        private void btnLast_Click(object sender, RoutedEventArgs e)

        {

            OnPageChanging(this.PageCount);

        }

        ///

        /// 点击跳转按钮

        ///

        ///

        ///

        private void btnGoTo_Click(object sender, RoutedEventArgs e)

        {

            int pageIndex = 1;

            try

            {

                pageIndex = Convert.ToInt32(txtPageIndex.Text);

            }

            catch

            {

 

            }

            finally

            {

                OnPageChanging(pageIndex);

            }

        }

        ///

        /// 页码更改

        ///

        ///

        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 };

            if (this.PageChanging != null)

            {

                this.PageChanging(this, eventArgs);

            }

            if (!eventArgs.IsCancel)

            {

                this.PageIndex = newPageIndex;

                if (this.PageChanged != null)

                {

                    this.PageChanged.Invoke(thisnew PageChangedEventArgs() { CurrentPageIndex = this.PageIndex });

                }

            }

        }

        ///

        /// 通知导航按钮(首页,上一页,下一页,末页)状态的更改

        ///

        void ChangeNavigationButtonState()

        {

            this.NotifyPropertyChanged("CanGoFirstOrPrev");

            this.NotifyPropertyChanged("CanGoLastOrNext");

        }

        ///

        /// 初始化数据

        ///

        void InitData()

        {

            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;

            }

            if (this.PageSize < 1)

            {

                this.PageSize = 20;

            }

        }

        #region INotifyPropertyChanged成员

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)

        {

            if (this.PropertyChanged != null)

            {

                this.PropertyChanged.Invoke(thisnew PropertyChangedEventArgs(propertyName));

            }

        }

        #endregion

    }

Xaml中使用如下:

<my:DataPager PageChanged="dataPager1_PageChanged" PageChanging="dataPager1_PageChanging"  TotalCount="{Binding TotalCount}" PageSize="20" PageIndex="{Binding PageIndex}" x:Name="dataPager1" VerticalAlignment="Top" />

下载位置:http://pan.baidu.com/s/1c26s3DY

时间: 2024-10-20 15:45:43

WPF DataPager控件的相关文章

wpf 修改控件Background

以TextBox 控件为例 一  Brushes.颜色 textBoxName.Background = Brushes.Blue; 二 背景色值#FFD2D2D2 1 .textBoxName.Background=new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD2D2D2")); 2.textBoxName.Background = new SolidColorBrush(Colors.White);

WPF获取控件内部的ScrollViewer,并控制ScrollViewer操作

//获取内部  ScrollViewer方法 public static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject        {            if (obj != null)            {                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)          

asp.net,关于Listview+DataPager控件使用

关于Listview+DataPager控件使用1.DAL层,根据开始条数+结束条数查询数据.2.BLL层,startRowIndex和maximumRows进行查询.(startRowIndex + 1, maximumRows + startRowIndex + 1);3.使用ObjectDataSource控件,配置数据源,   startRowIndex和maximumRows必须BLL逻辑层定义的方法参数名一致,   SelectCountMethod中添加查询总记录条数方法名.4.使

WPF 布局控件 之 DockPanel

DockPanel为容器控件 主要了解其Dock属性和LastChildFill属性的使用 一.LastChildFill="True" 时 代码: <DockPanel LastChildFill="True"> <Button DockPanel.Dock="Top">Top</Button> <Button DockPanel.Dock="Bottom">Bottom<

WPF条形码控件支持大多数流行的一维和二维条形码Barcode Professional

Barcode Professional for WPF是一款轻量级的 .NET 程序集,为你的WPF程序生成高质量的基于矢量的条码控件,支持大多数流行的一维和二维条形码:Code 39, Code 128, GS1-128, GS1 DataBar (RSS-14),  EAN 13 & UPC, Postal (USPS, British Royal Mail, Australia Post, DHL, etc.), Data Matrix, QR Code, PDF 417, UPS Ma

WPF Popup 控件导致被遮挡内容不刷新的原因

WPF Popup 控件导致被遮挡内容不刷新的原因 周银辉 今天在写一个WPF控件时用到了Popup控件,很郁闷的情况是:当popup关闭时,原来被popup挡住的界面部分不刷新,非要手动刷新一下(比如最大最小化一下窗口),就连网上传说的这个方法也不行 ? 1 2 3 4 5 6 7 8 9 10 public static class UiHelper {     private delegate void NoArgDelegate();     public static void Ref

Silverlight中DataPager控件扩展

大家一定遇到这样的情况,想改变一下SL的DataPager的显示信息,比如希望分页控件上显示数据的总数.那么就需要扩展一下DataPager控件即可. 其实扩展DataPager很简单,只要获取到DataPager控件上的元素,然后再改变元素上数据.比如DataPager控件上显示“总页数”的元素是一个TextBlock,那么可以通过方法GetTemplateChild获取到,参数是元素的名称.然后通过重写方法OnApplyTemplate即可,下面请看代码 代码 Code highlighti

WPF布局控件常用属性介绍

WPF布局控件常用属性介绍 其它 | 作者:慧都控件网 | 2011-04-06 13:41:57| 阅读 0次 有用(0) 评论(0) 概述:WPF布局控件都是派生自System.Windows.Controls.Panel抽象类的面板,Panel类继承自 FrameworkElement,Panel类本身并没有什么特别的,但是WPF中提供了许多用于布局的控件都继承自Panel类,如 StackPanel控件,WrapPanel,DockPanel,Grid,UniformGrid,Canva

WPF常用控件总结及其应用demo

WPF常用控件总结及其应用 一.控件 1.WrapPanel布局控件:可以实现当空间不足时子控件自动往下一行布局,空间充足时又会自动调整行布局.常用布局控件还有StackPanel(设置其子元素是垂直排列还是水平排列).Grid(通过定义行和列来绘制出一个表格).Canvas(通过指定相对于其的坐标来指定子控件的位置).DockPanel(设置其子元素如何停靠,DockPanel.Left.DockPanel.Right.DockPanel.Top.DockPanel.Bottom). 2.Sc