Pagination.cs 9.0 KB

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