DataPager.xaml.cs
8.48 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace HHECS.Controls
{
/// <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(20, (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="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 };
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 });
}
}
}
/// <summary>
/// 通知导航按钮(首页,上一页,下一页,末页)状态的更改
/// </summary>
void ChangeNavigationButtonState()
{
this.NotifyPropertyChanged("CanGoFirstOrPrev");
this.NotifyPropertyChanged("CanGoLastOrNext");
}
/// <summary>
/// 初始化数据
/// </summary>
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
}
public delegate void PageChangingRouteEventHandler(object sender, PageChangingEventArgs e);
public delegate void PageChangedRouteEventHandler(object sender, PageChangedEventArgs e);
}