Pagination.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. namespace Venus_Themes.CustomControls
  11. {
  12. public class Pagination : Control
  13. {
  14. private Venus_Themes.UserControls.PathButton _btnPrev = null;
  15. private Button _btnOne = null;
  16. private Button _btnDotPrev = null;
  17. private Button _btnCenterOne = null;
  18. private Button _btnCenterTwo = null;
  19. private Button _btnCenterThree = null;
  20. private Button _btnCenterFour = null;
  21. private Button _btnCenterFive = null;
  22. private Button _btnDotNext = null;
  23. private Button _btnLast = null;
  24. private Venus_Themes.UserControls.PathButton _btnNext = null;
  25. public Action<int> CurrentPageChanged;
  26. public int PageCount
  27. {
  28. get { return (int)GetValue(PageCountProperty); }
  29. set
  30. {
  31. SetValue(PageCountProperty, value);
  32. }
  33. }
  34. public static readonly DependencyProperty PageCountProperty =
  35. DependencyProperty.Register("PageCount", typeof(int), typeof(Pagination), new PropertyMetadata(1, (d, e) =>
  36. {
  37. if (!(d is Pagination pagination)) return;
  38. var page = (int)e.NewValue;
  39. pagination.IsSimple = page < 6;
  40. }));
  41. public bool IsSimple
  42. {
  43. get { return (bool)GetValue(IsSimpleProperty); }
  44. set { SetValue(IsSimpleProperty, value); }
  45. }
  46. public static readonly DependencyProperty IsSimpleProperty =
  47. DependencyProperty.Register("IsSimple", typeof(bool), typeof(Pagination), new PropertyMetadata(false));
  48. public int CurrentPage
  49. {
  50. get { return (int)GetValue(CurrentPageProperty); }
  51. set
  52. {
  53. SetValue(CurrentPageProperty, value);
  54. CurrentPageChanged(value);
  55. }
  56. }
  57. public static readonly DependencyProperty CurrentPageProperty =
  58. DependencyProperty.Register("CurrentPage", typeof(int), typeof(Pagination), new PropertyMetadata(1, (d, e) =>
  59. {
  60. if (!(d is Pagination pagination)) return;
  61. if (pagination.PageCount > 5)
  62. {
  63. pagination.UpdateControl();
  64. }
  65. else
  66. {
  67. pagination.UpdateControlSimple();
  68. }
  69. }));
  70. public override void OnApplyTemplate()
  71. {
  72. base.OnApplyTemplate();
  73. if (PageCount > 5)
  74. {
  75. InitControls();
  76. }
  77. else
  78. {
  79. InitControlsSimple();
  80. }
  81. }
  82. private List<Button> _simpleButtons = new List<Button>();
  83. private void InitControlsSimple()
  84. {
  85. _btnPrev = GetTemplateChild("btnPrev") as Venus_Themes.UserControls.PathButton;
  86. if (_btnPrev == null)
  87. {
  88. return;
  89. }
  90. _btnCenterOne = GetTemplateChild("btnCenterOne") as Button;
  91. _btnCenterTwo = GetTemplateChild("btnCenterTwo") as Button;
  92. _btnCenterThree = GetTemplateChild("btnCenterThree") as Button;
  93. _btnCenterFour = GetTemplateChild("btnCenterFour") as Button;
  94. _btnCenterFive = GetTemplateChild("btnCenterFive") as Button;
  95. _btnNext = GetTemplateChild("btnNext") as Venus_Themes.UserControls.PathButton;
  96. _simpleButtons.Clear();
  97. _simpleButtons.Add(_btnCenterOne);
  98. _simpleButtons.Add(_btnCenterTwo);
  99. _simpleButtons.Add(_btnCenterThree);
  100. _simpleButtons.Add(_btnCenterFour);
  101. _simpleButtons.Add(_btnCenterFive);
  102. BindClickSimple();
  103. UpdateControlSimple();
  104. }
  105. private void UpdateControlSimple()
  106. {
  107. if (_btnCenterOne == null)
  108. {
  109. return;
  110. }
  111. _btnCenterOne.Visibility = PageCount >= 1 ? Visibility.Visible : Visibility.Collapsed;
  112. _btnCenterTwo.Visibility = PageCount >= 2 ? Visibility.Visible : Visibility.Collapsed;
  113. _btnCenterThree.Visibility = PageCount >= 3 ? Visibility.Visible : Visibility.Collapsed;
  114. _btnCenterFour.Visibility = PageCount >= 4 ? Visibility.Visible : Visibility.Collapsed;
  115. _btnCenterFive.Visibility = PageCount >= 5 ? Visibility.Visible : Visibility.Collapsed;
  116. _btnPrev.IsEnabled = CurrentPage > 1;
  117. _btnNext.IsEnabled = CurrentPage < PageCount;
  118. _btnCenterOne.Background = _btnCenterTwo.Background = _btnCenterThree.Background = _btnCenterFour.Background = _btnCenterFive.Background = Brushes.LightBlue;
  119. _simpleButtons[CurrentPage - 1].Background = Brushes.Green;
  120. }
  121. private void BindClickSimple()
  122. {
  123. _btnPrev.Click += (s, e) => CurrentPage -= 1;
  124. _btnCenterOne.Click += (s, e) => CurrentPage = 1;
  125. _btnCenterTwo.Click += (s, e) => CurrentPage = 2;
  126. _btnCenterThree.Click += (s, e) => CurrentPage = 3;
  127. _btnCenterFour.Click += (s, e) => CurrentPage = 4;
  128. _btnCenterFive.Click += (s, e) => CurrentPage = 5;
  129. _btnNext.Click += (s, e) => CurrentPage += 1;
  130. }
  131. private void InitControls()
  132. {
  133. _btnPrev = GetTemplateChild("btnPrev") as Venus_Themes.UserControls.PathButton;
  134. _btnPrev.Cursor = Cursors.Hand;
  135. _btnOne = GetTemplateChild("btnOne") as Button;
  136. _btnDotPrev = GetTemplateChild("btnDotPrev") as Button;
  137. _btnCenterOne = GetTemplateChild("btnCenterOne") as Button;
  138. _btnCenterTwo = GetTemplateChild("btnCenterTwo") as Button;
  139. _btnCenterThree = GetTemplateChild("btnCenterThree") as Button;
  140. _btnCenterFour = GetTemplateChild("btnCenterFour") as Button;
  141. _btnCenterFive = GetTemplateChild("btnCenterFive") as Button;
  142. _btnDotNext = GetTemplateChild("btnDotNext") as Button;
  143. _btnLast = GetTemplateChild("btnLast") as Button;
  144. _btnNext = GetTemplateChild("btnNext") as Venus_Themes.UserControls.PathButton;
  145. _btnNext.Cursor = Cursors.Hand;
  146. BindClick();
  147. UpdateControl();
  148. }
  149. private void BindClick()
  150. {
  151. if (_btnPrev == null)
  152. {
  153. return;
  154. }
  155. _btnPrev.Click += (s, e) => SetIndex(-1);
  156. _btnOne.Click += (s, e) => SetIndex(1 - CurrentPage);
  157. _btnDotPrev.Click += (s, e) => SetIndex(-3);
  158. _btnCenterOne.Click += (s, e) => SetIndex(-2);
  159. _btnCenterTwo.Click += (s, e) => SetIndex(-1);
  160. _btnCenterFour.Click += (s, e) => SetIndex(1);
  161. _btnCenterFive.Click += (s, e) => SetIndex(2);
  162. _btnDotNext.Click += (s, e) => SetIndex(3);
  163. _btnLast.Click += (s, e) => SetIndex(PageCount - CurrentPage);
  164. _btnNext.Click += (s, e) => SetIndex(1);
  165. }
  166. public void SetIndex(int page)
  167. {
  168. if (page < 0)
  169. {
  170. if (CurrentPage + page > 0)
  171. {
  172. CurrentPage += page;
  173. }
  174. }
  175. else if (page > 0)
  176. {
  177. if (CurrentPage + page <= PageCount)
  178. {
  179. CurrentPage += page;
  180. }
  181. }
  182. }
  183. private void UpdateControl()
  184. {
  185. if (_btnPrev == null)
  186. {
  187. return;
  188. }
  189. _btnPrev.IsEnabled = CurrentPage > 1;
  190. _btnOne.Visibility = CurrentPage < 4 ? Visibility.Collapsed : Visibility.Visible;
  191. _btnDotPrev.Visibility = CurrentPage < 4 ? Visibility.Collapsed : Visibility.Visible;
  192. _btnCenterOne.Visibility = CurrentPage != 3 && CurrentPage != PageCount ? Visibility.Collapsed : Visibility.Visible;
  193. _btnCenterTwo.Visibility = CurrentPage == 1 || (PageCount - CurrentPage) == 2 ? Visibility.Collapsed : Visibility.Visible;
  194. _btnCenterFour.Visibility = CurrentPage == 3 || CurrentPage == PageCount ? Visibility.Collapsed : Visibility.Visible;
  195. _btnCenterFive.Visibility = CurrentPage != 1 && (PageCount - CurrentPage) != 2 ? Visibility.Collapsed : Visibility.Visible;
  196. _btnDotNext.Visibility = PageCount - CurrentPage < 3 ? Visibility.Collapsed : Visibility.Visible;
  197. _btnLast.Visibility = PageCount - CurrentPage < 3 ? Visibility.Collapsed : Visibility.Visible;
  198. _btnNext.IsEnabled = CurrentPage != PageCount;
  199. _btnOne.Content = 1;
  200. _btnCenterOne.Content = CurrentPage - 2;
  201. _btnCenterTwo.Content = CurrentPage - 1;
  202. _btnCenterThree.Content = CurrentPage;
  203. _btnCenterFour.Content = CurrentPage + 1;
  204. _btnCenterFive.Content = CurrentPage + 2;
  205. _btnLast.Content = PageCount;
  206. }
  207. }
  208. }