PageNumControl.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace AIRS.usercontrol
  18. {
  19. /// <summary>
  20. /// 分页控件
  21. /// </summary>
  22. public partial class PageNumControl: UserControl, INotifyPropertyChanged
  23. {
  24. #region 事件
  25. /// <summary>
  26. /// 分页事件
  27. /// </summary>
  28. public event EventHandler<PageChangedEventArgs> PageChanged;
  29. #endregion
  30. #region 变量
  31. private ObservableCollection<PageControlItemModel> _collection = new ObservableCollection<PageControlItemModel>();
  32. private List<PageControlItemModel> _list = null;
  33. #endregion
  34. #region 属性
  35. private int _FontSize = 36;
  36. /// <summary>
  37. /// 文字字体大小
  38. /// </summary>
  39. public int FontSize
  40. {
  41. get { return _FontSize; }
  42. set
  43. {
  44. _FontSize = value;
  45. OnPropertyChanged("FontSize");
  46. CalcPageNumList(); //计算页码
  47. }
  48. }
  49. #endregion
  50. #region 分页相关属性
  51. private int _PageCount = 1;
  52. /// <summary>
  53. /// 总页数
  54. /// </summary>
  55. public int PageCount
  56. {
  57. get { return _PageCount; }
  58. set
  59. {
  60. _PageCount = value;
  61. OnPropertyChanged("PageCount");
  62. }
  63. }
  64. private int _Page = 1;
  65. /// <summary>
  66. /// 当前页码
  67. /// </summary>
  68. public int Page
  69. {
  70. get { return _Page; }
  71. set
  72. {
  73. _Page = value;
  74. OnPropertyChanged("Page");
  75. CalcPageNumList(); //计算页码
  76. }
  77. }
  78. private int _RecordCount = 0;
  79. /// <summary>
  80. /// 记录总数
  81. /// </summary>
  82. public int RecordCount
  83. {
  84. get { return _RecordCount; }
  85. set
  86. {
  87. _RecordCount = value;
  88. OnPropertyChanged("RecordCount");
  89. CalcPageNumList(); //计算页码
  90. }
  91. }
  92. private int _PageSize = 10;
  93. /// <summary>
  94. /// 每页记录数
  95. /// </summary>
  96. public int PageSize
  97. {
  98. get { return _PageSize; }
  99. set
  100. {
  101. _PageSize = value;
  102. OnPropertyChanged("PageSize");
  103. CalcPageNumList(); //计算页码
  104. }
  105. }
  106. private int _ContinuousCount = 3;
  107. /// <summary>
  108. /// 当前页码右边连续页码数
  109. /// </summary>
  110. public int ContinuousCount
  111. {
  112. get { return _ContinuousCount; }
  113. set
  114. {
  115. _ContinuousCount = value;
  116. OnPropertyChanged("_ContinuousCount");
  117. CalcPageNumList(); //计算页码
  118. }
  119. }
  120. #endregion
  121. #region 构造函数
  122. public PageNumControl()
  123. {
  124. InitializeComponent();
  125. }
  126. #endregion
  127. #region 单击页码事件
  128. private void btnNum_Click(object sender, RoutedEventArgs e)
  129. {
  130. if (PageChanged != null)
  131. {
  132. Button btn = sender as Button;
  133. PageControlItemModel itemModel = btn.CommandParameter as PageControlItemModel;
  134. if (itemModel.Page != Page)
  135. {
  136. Page = itemModel.Page;
  137. CalcPageNumList();
  138. PageChangedEventArgs args = new PageChangedEventArgs(itemModel.Page);
  139. PageChanged(sender, args);
  140. }
  141. }
  142. }
  143. #endregion
  144. #region 计算页码
  145. /// <summary>
  146. /// 计算页码
  147. /// </summary>
  148. private void CalcPageNumList()
  149. {
  150. PageCount = (RecordCount - 1) / PageSize + 1; //计算总页数PageCount
  151. _list = new List<PageControlItemModel>();
  152. //第一页
  153. PageControlItemModel item = new PageControlItemModel(1, Page);
  154. _list.Add(item);
  155. //当前页码连续页码
  156. for (int i = Page - ContinuousCount; i <= Page + ContinuousCount; i++)
  157. {
  158. if (i > 0 && i < PageCount)
  159. {
  160. item = new PageControlItemModel(i, Page);
  161. if (!_list.Exists(a => a.Page == item.Page))
  162. {
  163. _list.Add(item);
  164. }
  165. }
  166. }
  167. //最后一页
  168. item = new PageControlItemModel(PageCount, Page);
  169. if (!_list.Exists(a => a.Page == item.Page))
  170. {
  171. _list.Add(item);
  172. }
  173. for (int i = _list.Count - 1; i > 0; i--)
  174. {
  175. if (_list[i].Page - _list[i - 1].Page > 1)
  176. {
  177. _list.Insert(i, new PageControlItemModel(0, Page, 2));
  178. }
  179. }
  180. //上一页下一页
  181. if (Page == 1)
  182. {
  183. this.btnPrePage.IsEnabled = false;
  184. this.btnPrePage.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#88dddddd"));
  185. }
  186. else
  187. {
  188. this.btnPrePage.IsEnabled = true;
  189. this.btnPrePage.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#fff"));
  190. }
  191. if (Page == PageCount)
  192. {
  193. this.btnNextPage.IsEnabled = false;
  194. this.btnNextPage.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#88dddddd"));
  195. }
  196. else
  197. {
  198. this.btnNextPage.IsEnabled = true;
  199. this.btnNextPage.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#fff"));
  200. }
  201. _collection.Clear();
  202. _list.ForEach(a => { _collection.Add(a); });
  203. this.itemsControl.ItemsSource = _collection;
  204. }
  205. #endregion
  206. #region 上一页
  207. private void btnPrePage_Click(object sender, RoutedEventArgs e)
  208. {
  209. int prePage = Page - 1;
  210. if (prePage < 1) prePage = 1;
  211. if (prePage != Page)
  212. {
  213. Page = prePage;
  214. CalcPageNumList();
  215. PageChangedEventArgs args = new PageChangedEventArgs(prePage);
  216. PageChanged(sender, args);
  217. }
  218. }
  219. #endregion
  220. #region 下一页
  221. private void btnNextPage_Click(object sender, RoutedEventArgs e)
  222. {
  223. int nextPage = Page + 1;
  224. if (nextPage > PageCount) nextPage = PageCount;
  225. if (nextPage != Page)
  226. {
  227. Page = nextPage;
  228. CalcPageNumList();
  229. PageChangedEventArgs args = new PageChangedEventArgs(nextPage);
  230. PageChanged(sender, args);
  231. }
  232. }
  233. #endregion
  234. #region INotifyPropertyChanged接口
  235. public event PropertyChangedEventHandler PropertyChanged;
  236. protected void OnPropertyChanged(string name)
  237. {
  238. if (PropertyChanged != null)
  239. {
  240. PropertyChanged(this, new PropertyChangedEventArgs(name));
  241. }
  242. }
  243. #endregion
  244. }
  245. #region 分页控件Item Model
  246. /// <summary>
  247. /// 分页控件Item Model
  248. /// </summary>
  249. public class PageControlItemModel : INotifyPropertyChanged
  250. {
  251. private int _Type = 1;
  252. /// <summary>
  253. /// 类型(1数字 2省略号)
  254. /// </summary>
  255. public int Type
  256. {
  257. get { return _Type; }
  258. set
  259. {
  260. _Type = value;
  261. OnPropertyChanged("Type");
  262. if (_Type == 1)
  263. {
  264. NumVisible = Visibility.Visible;
  265. OmitVisible = Visibility.Collapsed;
  266. }
  267. else
  268. {
  269. NumVisible = Visibility.Collapsed;
  270. OmitVisible = Visibility.Visible;
  271. }
  272. }
  273. }
  274. private bool _IsCurrentPage;
  275. /// <summary>
  276. /// 是否当前页码
  277. /// </summary>
  278. public bool IsCurrentPage
  279. {
  280. get { return _IsCurrentPage; }
  281. set
  282. {
  283. _IsCurrentPage = value;
  284. OnPropertyChanged("IsCurrentPage");
  285. if (_IsCurrentPage)
  286. {
  287. CurrentPageColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#00f0ff"));
  288. }
  289. else
  290. {
  291. CurrentPageColor = new SolidColorBrush(Colors.White);
  292. }
  293. }
  294. }
  295. private SolidColorBrush _CurrentPageColor = new SolidColorBrush(Colors.White);
  296. /// <summary>
  297. /// 当前页码颜色
  298. /// </summary>
  299. public SolidColorBrush CurrentPageColor
  300. {
  301. get { return _CurrentPageColor; }
  302. set
  303. {
  304. _CurrentPageColor = value;
  305. OnPropertyChanged("CurrentPageColor");
  306. }
  307. }
  308. private int _Page;
  309. /// <summary>
  310. /// 页码
  311. /// </summary>
  312. public int Page
  313. {
  314. get { return _Page; }
  315. set
  316. {
  317. _Page = value;
  318. OnPropertyChanged("Page");
  319. }
  320. }
  321. private Visibility _NumVisible = Visibility.Visible;
  322. /// <summary>
  323. /// 数字可见
  324. /// </summary>
  325. public Visibility NumVisible
  326. {
  327. get { return _NumVisible; }
  328. set
  329. {
  330. _NumVisible = value;
  331. OnPropertyChanged("NumVisible");
  332. }
  333. }
  334. private Visibility _OmitVisible = Visibility.Collapsed;
  335. /// <summary>
  336. /// 省略号可见
  337. /// </summary>
  338. public Visibility OmitVisible
  339. {
  340. get { return _OmitVisible; }
  341. set
  342. {
  343. _OmitVisible = value;
  344. OnPropertyChanged("OmitVisible");
  345. }
  346. }
  347. /// <summary>
  348. /// 分页控件Item Model
  349. /// </summary>
  350. /// <param name="page">页码</param>
  351. /// <param name="currentPage">当前页码</param>
  352. /// <param name="type">类型(1数字 2省略号)</param>
  353. public PageControlItemModel(int page, int currentPage, int type = 1)
  354. {
  355. Type = type;
  356. Page = page;
  357. IsCurrentPage = page == currentPage;
  358. }
  359. #region INotifyPropertyChanged接口
  360. public event PropertyChangedEventHandler PropertyChanged;
  361. protected void OnPropertyChanged(string name)
  362. {
  363. if (PropertyChanged != null)
  364. {
  365. PropertyChanged(this, new PropertyChangedEventArgs(name));
  366. }
  367. }
  368. #endregion
  369. }
  370. #endregion
  371. #region 分页事件参数
  372. /// <summary>
  373. /// 分页事件参数
  374. /// </summary>
  375. public class PageChangedEventArgs : EventArgs
  376. {
  377. private int _Page = 1;
  378. /// <summary>
  379. /// 当前页码
  380. /// </summary>
  381. public int Page
  382. {
  383. get
  384. {
  385. return _Page;
  386. }
  387. }
  388. /// <summary>
  389. /// 分页事件参数
  390. /// </summary>
  391. /// <param name="page">当前页码</param>
  392. public PageChangedEventArgs(int page)
  393. {
  394. _Page = page;
  395. }
  396. }
  397. #endregion
  398. }