Pagination.cs 8.9 KB

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