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的绑定.

 

核心代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
///
    /// 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 == null) return;
                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 == null) return;
                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(this, new 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(this, new 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" />

 

源码下载

 

注意:在使用MVVM框架的情况下,如果在vm里获取分页相关的数据,需要使用双向绑定,比如:PageIndex="{Binding PageIndex,Mode=TwoWay}"

 

出处:https://www.cnblogs.com/scheshan/archive/2012/06/19/2554550.html

=======================================================================

推荐WPF相关文章:

 

 

posted on 2020-03-24 18:57  jack_Meng  阅读(...)  评论(...编辑  收藏

导航