WPF版分页控件
好多年没有发博了,今天闲暇,把给公司做的ERP分页控件拿出来,有很多都用到分页的,所以就封装了一个UserControl分页控件,效果还不错,可以看看,先!
注意:有用到存储过程。
如果还不明白,请联系我!
效果图:
![]()
界面设置
界面逻辑
1 /// <summary>
2 /// 申明委托
3 /// </summary>
4 /// <param name="e"></param>
5 /// <returns></returns>
6 public delegate void EventPagingHandler(EventPagingArg e);
7
8 /// <summary>
9 /// UCPaging.xaml 的交互逻辑
10 /// </summary>
11 public partial class UCPager : UserControl
12 {
13 #region Pirvate Member
14
15 public Pager pager;
16 public DataTable dt;
17 public event EventPagingHandler EventPaging;
18
19 #endregion
20
21 /// <summary>
22 /// 无参构造
23 /// </summary>
24 public UCPager()
25 {
26 InitializeComponent();
27 pager = new Pager();
28 }
29
30 /// <summary>
31 /// 得到数据
32 /// </summary>
33 /// <param name="tableName"></param>
34 /// <param name="fldName"></param>
35 /// <param name="fldSort"></param>
36 /// <param name="strCondition"></param>
37 /// <returns></returns>
38 public void Bind()
39 {
40 if (this.EventPaging != null)
41 this.EventPaging(new EventPagingArg());
42
43 if (dt == null || dt.Rows.Count == 0)
44 {
45 this.Visibility = Visibility.Collapsed;
46 return;
47 }
48 else
49 this.Visibility = Visibility.Visible;
50
51 //--控制
52 this.txbInfo.Text = "第1-" + dt.Rows.Count + "条 共" + pager.RecorderCount + "条 | 第" + pager.PageIndex + "页 共" + pager.PageCount + "页";
53 this.nudPageIndex.Minimum = 1;
54 this.nudPageIndex.Maximum = pager.PageCount;
55 this.nudPageIndex.Value = pager.PageIndex;
56 this.txbTotalPageCount.Text = " / "+pager.PageCount;
57 if (pager.PageCount > 1 && pager.PageCount > pager.PageIndex)
58 {
59 this.iNext.Source = new BitmapImage(new Uri("../Resources/Images/Pager/next2.gif", UriKind.Relative));
60 this.iLast.Source = new BitmapImage(new Uri("../Resources/Images/Pager/last2.gif", UriKind.Relative));
61 this.iNext.IsEnabled = true;
62 this.iLast.IsEnabled = true;
63 }
64 else
65 {
66 this.iNext.Source = new BitmapImage(new Uri("../Resources/Images/Pager/next1.gif", UriKind.Relative));
67 this.iLast.Source = new BitmapImage(new Uri("../Resources/Images/Pager/last1.gif", UriKind.Relative));
68 this.iNext.IsEnabled = false;
69 this.iLast.IsEnabled = false;
70 }
71
72 if (pager.PageIndex > 1 && pager.PageIndex <= pager.PageCount)
73 {
74 this.iFirst.IsEnabled = true;
75 this.iPrev.IsEnabled = true;
76 this.iFirst.Source = new BitmapImage(new Uri("../Resources/Images/Pager/first2.gif", UriKind.Relative));
77 this.iPrev.Source = new BitmapImage(new Uri("../Resources/Images/Pager/previous2.gif", UriKind.Relative));
78 }
79 else
80 {
81 this.iFirst.Source = new BitmapImage(new Uri("../Resources/Images/Pager/first1.gif", UriKind.Relative));
82 this.iPrev.Source = new BitmapImage(new Uri("../Resources/Images/Pager/previous1.gif", UriKind.Relative));
83 this.iFirst.IsEnabled = false;
84 this.iPrev.IsEnabled = false;
85 }
86 }
87
88 /// <summary>
89 /// 得到数据
90 /// </summary>
91 /// <param name="tableName">表名</param>
92 /// <param name="fldName">要查询的字段(所有为*)</param>
93 /// <param name="fldSort">排序</param>
94 /// <param name="strCondition">Where条件</param>
95 /// <returns>返回DataTable,如要绑定控件,可自己转换成IList</returns>
96 public DataTable GetData(string tableName, string fldName, string fldSort,
97 string strCondition)
98 {
99 int pageCount = 0;
100 int recordCount = 0;
101 dt = PagingManage.GetDataSet(tableName, fldName, pager.PageSize, pager.PageIndex, fldSort,
102 strCondition, out pageCount, out recordCount);
103 pager.PageCount = pageCount;
104 pager.RecorderCount = recordCount;
105
106 return dt;
107 }
108
109 /// <summary>
110 /// 第一页
111 /// </summary>
112 /// <param name="sender"></param>
113 /// <param name="e"></param>
114 public void iFirst_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
115 {
116 this.pager.PageIndex = 1;
117 this.Bind();
118 }
119
120 /// <summary>
121 /// 上一页
122 /// </summary>
123 /// <param name="sender"></param>
124 /// <param name="e"></param>
125 public void iPrev_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
126 {
127 this.pager.PageIndex--;
128 this.Bind();
129 }
130
131 /// <summary>
132 /// 下一页
133 /// </summary>
134 /// <param name="sender"></param>
135 /// <param name="e"></param>
136 public void iNext_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
137 {
138 this.pager.PageIndex++;
139 this.Bind();
140 }
141
142 /// <summary>
143 /// 末页
144 /// </summary>
145 /// <param name="sender"></param>
146 /// <param name="e"></param>
147 public void iLast_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
148 {
149 this.pager.PageIndex = pager.PageCount;
150 this.Bind();
151 }
152
153 /// <summary>
154 /// 确定导航到指定页
155 /// </summary>
156 /// <param name="sender"></param>
157 /// <param name="e"></param>
158 private void nudPageIndex_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
159 {
160 if (e.KeyCode == System.Windows.Forms.Keys.Return)
161 {
162 this.pager.PageIndex = int.Parse(this.nudPageIndex.Value.ToString());
163 this.Bind();
164 }
165 }
166
167 /// <summary>
168 /// 确定导航
169 /// </summary>
170 /// <param name="sender"></param>
171 /// <param name="e"></param>
172 private void txbGO_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
173 {
174 this.pager.PageIndex = int.Parse(this.nudPageIndex.Value.ToString());
175 this.Bind();
176 }
177 }
178
179 /// <summary>
180 /// 自定义事件数据基类
181 /// </summary>
182 public class EventPagingArg : EventArgs
183 {
184 public EventPagingArg() { }
185 }
2 /// 申明委托
3 /// </summary>
4 /// <param name="e"></param>
5 /// <returns></returns>
6 public delegate void EventPagingHandler(EventPagingArg e);
7
8 /// <summary>
9 /// UCPaging.xaml 的交互逻辑
10 /// </summary>
11 public partial class UCPager : UserControl
12 {
13 #region Pirvate Member
14
15 public Pager pager;
16 public DataTable dt;
17 public event EventPagingHandler EventPaging;
18
19 #endregion
20
21 /// <summary>
22 /// 无参构造
23 /// </summary>
24 public UCPager()
25 {
26 InitializeComponent();
27 pager = new Pager();
28 }
29
30 /// <summary>
31 /// 得到数据
32 /// </summary>
33 /// <param name="tableName"></param>
34 /// <param name="fldName"></param>
35 /// <param name="fldSort"></param>
36 /// <param name="strCondition"></param>
37 /// <returns></returns>
38 public void Bind()
39 {
40 if (this.EventPaging != null)
41 this.EventPaging(new EventPagingArg());
42
43 if (dt == null || dt.Rows.Count == 0)
44 {
45 this.Visibility = Visibility.Collapsed;
46 return;
47 }
48 else
49 this.Visibility = Visibility.Visible;
50
51 //--控制
52 this.txbInfo.Text = "第1-" + dt.Rows.Count + "条 共" + pager.RecorderCount + "条 | 第" + pager.PageIndex + "页 共" + pager.PageCount + "页";
53 this.nudPageIndex.Minimum = 1;
54 this.nudPageIndex.Maximum = pager.PageCount;
55 this.nudPageIndex.Value = pager.PageIndex;
56 this.txbTotalPageCount.Text = " / "+pager.PageCount;
57 if (pager.PageCount > 1 && pager.PageCount > pager.PageIndex)
58 {
59 this.iNext.Source = new BitmapImage(new Uri("../Resources/Images/Pager/next2.gif", UriKind.Relative));
60 this.iLast.Source = new BitmapImage(new Uri("../Resources/Images/Pager/last2.gif", UriKind.Relative));
61 this.iNext.IsEnabled = true;
62 this.iLast.IsEnabled = true;
63 }
64 else
65 {
66 this.iNext.Source = new BitmapImage(new Uri("../Resources/Images/Pager/next1.gif", UriKind.Relative));
67 this.iLast.Source = new BitmapImage(new Uri("../Resources/Images/Pager/last1.gif", UriKind.Relative));
68 this.iNext.IsEnabled = false;
69 this.iLast.IsEnabled = false;
70 }
71
72 if (pager.PageIndex > 1 && pager.PageIndex <= pager.PageCount)
73 {
74 this.iFirst.IsEnabled = true;
75 this.iPrev.IsEnabled = true;
76 this.iFirst.Source = new BitmapImage(new Uri("../Resources/Images/Pager/first2.gif", UriKind.Relative));
77 this.iPrev.Source = new BitmapImage(new Uri("../Resources/Images/Pager/previous2.gif", UriKind.Relative));
78 }
79 else
80 {
81 this.iFirst.Source = new BitmapImage(new Uri("../Resources/Images/Pager/first1.gif", UriKind.Relative));
82 this.iPrev.Source = new BitmapImage(new Uri("../Resources/Images/Pager/previous1.gif", UriKind.Relative));
83 this.iFirst.IsEnabled = false;
84 this.iPrev.IsEnabled = false;
85 }
86 }
87
88 /// <summary>
89 /// 得到数据
90 /// </summary>
91 /// <param name="tableName">表名</param>
92 /// <param name="fldName">要查询的字段(所有为*)</param>
93 /// <param name="fldSort">排序</param>
94 /// <param name="strCondition">Where条件</param>
95 /// <returns>返回DataTable,如要绑定控件,可自己转换成IList</returns>
96 public DataTable GetData(string tableName, string fldName, string fldSort,
97 string strCondition)
98 {
99 int pageCount = 0;
100 int recordCount = 0;
101 dt = PagingManage.GetDataSet(tableName, fldName, pager.PageSize, pager.PageIndex, fldSort,
102 strCondition, out pageCount, out recordCount);
103 pager.PageCount = pageCount;
104 pager.RecorderCount = recordCount;
105
106 return dt;
107 }
108
109 /// <summary>
110 /// 第一页
111 /// </summary>
112 /// <param name="sender"></param>
113 /// <param name="e"></param>
114 public void iFirst_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
115 {
116 this.pager.PageIndex = 1;
117 this.Bind();
118 }
119
120 /// <summary>
121 /// 上一页
122 /// </summary>
123 /// <param name="sender"></param>
124 /// <param name="e"></param>
125 public void iPrev_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
126 {
127 this.pager.PageIndex--;
128 this.Bind();
129 }
130
131 /// <summary>
132 /// 下一页
133 /// </summary>
134 /// <param name="sender"></param>
135 /// <param name="e"></param>
136 public void iNext_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
137 {
138 this.pager.PageIndex++;
139 this.Bind();
140 }
141
142 /// <summary>
143 /// 末页
144 /// </summary>
145 /// <param name="sender"></param>
146 /// <param name="e"></param>
147 public void iLast_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
148 {
149 this.pager.PageIndex = pager.PageCount;
150 this.Bind();
151 }
152
153 /// <summary>
154 /// 确定导航到指定页
155 /// </summary>
156 /// <param name="sender"></param>
157 /// <param name="e"></param>
158 private void nudPageIndex_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
159 {
160 if (e.KeyCode == System.Windows.Forms.Keys.Return)
161 {
162 this.pager.PageIndex = int.Parse(this.nudPageIndex.Value.ToString());
163 this.Bind();
164 }
165 }
166
167 /// <summary>
168 /// 确定导航
169 /// </summary>
170 /// <param name="sender"></param>
171 /// <param name="e"></param>
172 private void txbGO_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
173 {
174 this.pager.PageIndex = int.Parse(this.nudPageIndex.Value.ToString());
175 this.Bind();
176 }
177 }
178
179 /// <summary>
180 /// 自定义事件数据基类
181 /// </summary>
182 public class EventPagingArg : EventArgs
183 {
184 public EventPagingArg() { }
185 }
浙公网安备 33010602011771号