PaginationUserControl.xaml.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. namespace ivf_tl_Manage.UserControls
  6. {
  7. /// <summary>
  8. /// PaginationUserControl.xaml 的交互逻辑
  9. /// </summary>
  10. public partial class PaginationUserControl : UserControl
  11. {
  12. public event Action<PaginationUserControl, int> PageChangedEvent;
  13. public event Action ClickPageEvent;
  14. private int scrNum = 2;
  15. public PaginationUserControl()
  16. {
  17. InitializeComponent();
  18. }
  19. public Thickness HeadMargin
  20. {
  21. get { return (Thickness)GetValue(HeadMarginProperty); }
  22. set { SetValue(HeadMarginProperty, value); }
  23. }
  24. public static readonly DependencyProperty HeadMarginProperty =
  25. DependencyProperty.Register("HeadMargin", typeof(Thickness), typeof(PaginationUserControl), new PropertyMetadata(new Thickness(40, 0, 0, 0)));
  26. public Thickness LastMargin
  27. {
  28. get { return (Thickness)GetValue(LastMarginProperty); }
  29. set { SetValue(LastMarginProperty, value); }
  30. }
  31. public static readonly DependencyProperty LastMarginProperty =
  32. DependencyProperty.Register("LastMargin", typeof(Thickness), typeof(PaginationUserControl), new PropertyMetadata(new Thickness(0, 0, 40, 0)));
  33. private void Ok_Click(object sender, RoutedEventArgs e)
  34. {
  35. }
  36. public int DefeatPage
  37. {
  38. get { return (int)GetValue(DefeatPageProperty); }
  39. set { SetValue(DefeatPageProperty, value); }
  40. }
  41. // Using a DependencyProperty as the backing store for DefeatPage. This enables animation, styling, binding, etc...
  42. public static readonly DependencyProperty DefeatPageProperty =
  43. DependencyProperty.Register("DefeatPage", typeof(int), typeof(PaginationUserControl), new PropertyMetadata(1));
  44. private static void dsf(DependencyObject d, DependencyPropertyChangedEventArgs e)
  45. {
  46. string ss = "";
  47. }
  48. public int PageSize
  49. {
  50. get { return (int)GetValue(PageSizeProperty); }
  51. set { SetValue(PageSizeProperty, value); }
  52. }
  53. public static readonly DependencyProperty PageSizeProperty =
  54. DependencyProperty.Register("PageSize", typeof(int), typeof(PaginationUserControl), new PropertyMetadata(-1, new PropertyChangedCallback(PageSizePropertyChangedCallback)));
  55. private static void PageSizePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  56. {
  57. if (!(d is PaginationUserControl source)) return;
  58. int newPageSize = (int)e.NewValue;
  59. source.WindowInit(newPageSize, source.DataCount);
  60. }
  61. public int DataCount
  62. {
  63. get { return (int)GetValue(DataCountProperty); }
  64. set { SetValue(DataCountProperty, value); }
  65. }
  66. public static readonly DependencyProperty DataCountProperty =
  67. DependencyProperty.Register("DataCount", typeof(int), typeof(PaginationUserControl), new PropertyMetadata(-1, new PropertyChangedCallback(DataCountPropertyChangedCallback)));
  68. private static void DataCountPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  69. {
  70. if (!(d is PaginationUserControl source)) return;
  71. int newDataCount = (int)e.NewValue;
  72. source.WindowInit(source.PageSize, newDataCount);
  73. }
  74. private void WindowInit(int newPageSize, int newDataCount)
  75. {
  76. if (newPageSize < 0 || newDataCount < 0) return;
  77. if (newPageSize == 0 || newDataCount == 0)
  78. {
  79. PageCount = 1;
  80. }
  81. else
  82. {
  83. int countPage = newDataCount / newPageSize;
  84. if (newDataCount % newPageSize != 0)
  85. {
  86. countPage++;
  87. }
  88. PageCount = countPage;
  89. }
  90. if (PageCount < 1) PageCount = 1;
  91. ObservableCollection<int> _items = new ObservableCollection<int>();
  92. for (int i = 1; i <= PageCount; i++)
  93. {
  94. _items.Add(i);
  95. }
  96. this.pageListBox.SelectedIndex = -1;
  97. this.pageListBox.ItemsSource = _items;
  98. var k = DefeatPage;
  99. if (k > PageCount) k = PageCount;
  100. if (k < 1) k = 1;
  101. test = true;
  102. this.pageListBox.SelectedIndex = k - 1;
  103. test = false;
  104. }
  105. public bool test = false;
  106. public int PageCount
  107. {
  108. get { return (int)GetValue(PageCountProperty); }
  109. set { SetValue(PageCountProperty, value); }
  110. }
  111. public static readonly DependencyProperty PageCountProperty =
  112. DependencyProperty.Register("PageCount", typeof(int), typeof(PaginationUserControl), new PropertyMetadata(-1));
  113. private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  114. {
  115. if (!(sender is ListBox listBox)) return;
  116. if (listBox.SelectedIndex == -1) return;
  117. if (listBox.SelectedIndex < scrNum || listBox.Items.Count - listBox.SelectedIndex - 1 < scrNum)
  118. {
  119. listBox.ScrollIntoView(listBox.Items[listBox.SelectedIndex]);
  120. if (!test) ClickPageEvent?.Invoke();
  121. PageChangedEvent?.Invoke(this, (listBox.SelectedIndex + 1));
  122. return;
  123. }
  124. var scr = listBox.Template.FindName("scroll", listBox) as ScrollViewer;
  125. if (scr == null)
  126. {
  127. listBox.ScrollIntoView(listBox.Items[listBox.SelectedIndex]);
  128. if (!test) ClickPageEvent?.Invoke();
  129. PageChangedEvent?.Invoke(this, (listBox.SelectedIndex + 1));
  130. return;
  131. }
  132. scr.ScrollToHorizontalOffset(listBox.SelectedIndex - scrNum);
  133. if (!test) ClickPageEvent?.Invoke();
  134. PageChangedEvent?.Invoke(this, (listBox.SelectedIndex + 1));
  135. }
  136. private void JumpPage_Click(object sender, RoutedEventArgs e)
  137. {
  138. if (int.TryParse(this.jumpPage_TextBox.Text.Trim(), out int newPage) && newPage >= 1 && newPage <= PageCount)
  139. {
  140. ClickPageEvent?.Invoke();
  141. this.pageListBox.SelectedIndex = newPage - 1;
  142. return;
  143. }
  144. return;
  145. }
  146. private void PageFirst_Click(object sender, RoutedEventArgs e)
  147. {
  148. if (!this.pageListBox.HasItems) return;
  149. if (this.pageListBox.SelectedIndex != 0)
  150. {
  151. ClickPageEvent?.Invoke();
  152. this.pageListBox.SelectedIndex = 0;
  153. }
  154. }
  155. private void PagePre_Click(object sender, RoutedEventArgs e)
  156. {
  157. if (!this.pageListBox.HasItems) return;
  158. if (this.pageListBox.SelectedIndex > 0)
  159. {
  160. ClickPageEvent?.Invoke();
  161. this.pageListBox.SelectedIndex--;
  162. }
  163. }
  164. private void PageNext_Click(object sender, RoutedEventArgs e)
  165. {
  166. if (!this.pageListBox.HasItems) return;
  167. if (this.pageListBox.SelectedIndex < (PageCount - 1))
  168. {
  169. ClickPageEvent?.Invoke();
  170. this.pageListBox.SelectedIndex++;
  171. }
  172. }
  173. private void PageLast_Click(object sender, RoutedEventArgs e)
  174. {
  175. if (!this.pageListBox.HasItems) return;
  176. if (this.pageListBox.SelectedIndex != (PageCount - 1))
  177. {
  178. ClickPageEvent?.Invoke();
  179. this.pageListBox.SelectedIndex = PageCount - 1;
  180. }
  181. }
  182. public string PageInfoString
  183. {
  184. get { return (string)GetValue(PageInfoStringProperty); }
  185. set { SetValue(PageInfoStringProperty, value); }
  186. }
  187. public static readonly DependencyProperty PageInfoStringProperty =
  188. DependencyProperty.Register("PageInfoString", typeof(string), typeof(PaginationUserControl), new PropertyMetadata("张图片"));
  189. public string PageUnit
  190. {
  191. get { return (string)GetValue(PageUnitProperty); }
  192. set { SetValue(PageUnitProperty, value); }
  193. }
  194. public static readonly DependencyProperty PageUnitProperty =
  195. DependencyProperty.Register("PageUnit", typeof(string), typeof(PaginationUserControl), new PropertyMetadata("张"));
  196. }
  197. }