CustomControlPagination.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using ivf_tl_Operate.Converts;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  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 ivf_tl_Operate.CustomUserControls
  18. {
  19. /// <summary>
  20. /// CustomControlPagination.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class CustomControlPagination : UserControl
  23. {
  24. public event Action<int> PageChangedEvent;
  25. public CustomControlPagination()
  26. {
  27. InitializeComponent();
  28. }
  29. /// <summary>
  30. /// 总页数
  31. /// </summary>
  32. private int countPage = 0;
  33. public int DefeatPage
  34. {
  35. get { return (int)GetValue(DefeatPageProperty); }
  36. set { SetValue(DefeatPageProperty, value); }
  37. }
  38. // Using a DependencyProperty as the backing store for DefeatPage. This enables animation, styling, binding, etc...
  39. public static readonly DependencyProperty DefeatPageProperty =
  40. DependencyProperty.Register("DefeatPage", typeof(int), typeof(CustomControlPagination), new PropertyMetadata(1));
  41. /// <summary>
  42. /// 总条目数量
  43. /// </summary>
  44. public int CountNum
  45. {
  46. get { return (int)GetValue(CountNumProperty); }
  47. set { SetValue(CountNumProperty, value); }
  48. }
  49. // Using a DependencyProperty as the backing store for CountNum. This enables animation, styling, binding, etc...
  50. public static readonly DependencyProperty CountNumProperty =
  51. DependencyProperty.Register("CountNum", typeof(int), typeof(CustomControlPagination), new PropertyMetadata(-1, new PropertyChangedCallback(CountNumPropertyChangedCallback)));
  52. /// <summary>
  53. /// 每一页数量
  54. /// </summary>
  55. public int PageNum
  56. {
  57. get { return (int)GetValue(PageNumProperty); }
  58. set { SetValue(PageNumProperty, value); }
  59. }
  60. // Using a DependencyProperty as the backing store for PageNum. This enables animation, styling, binding, etc...
  61. public static readonly DependencyProperty PageNumProperty =
  62. DependencyProperty.Register("PageNum", typeof(int), typeof(CustomControlPagination), new PropertyMetadata(0, new PropertyChangedCallback(PageNumPropertyChangedCallback)));
  63. private static void PageNumPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  64. {
  65. if (!(d is CustomControlPagination source)) return;
  66. int currentPageNum = (int)e.NewValue;
  67. source.WindowInit(currentPageNum, source.CountNum);
  68. }
  69. private static void CountNumPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  70. {
  71. if (!(d is CustomControlPagination source)) return;
  72. int max = (int)e.NewValue;
  73. source.WindowInit(source.PageNum, max);
  74. }
  75. private void WindowInit(int pageSize, int dataCount)
  76. {
  77. if (pageSize == -1 || dataCount == -1) return;
  78. if (dataCount == 0 || pageSize == 0)
  79. {
  80. PageChangedEvent?.Invoke(1);
  81. this.grid.Visibility = Visibility.Collapsed;
  82. return;
  83. }
  84. int countPage = dataCount / pageSize;
  85. if (dataCount % pageSize != 0)
  86. {
  87. countPage++;
  88. }
  89. ObservableCollection<int> _items = new ObservableCollection<int>();
  90. for (int i = 1; i <= countPage; i++)
  91. {
  92. _items.Add(i);
  93. }
  94. this.pageListBox.SelectedIndex = -1;
  95. this.pageListBox.ItemsSource = _items;
  96. this.countPage = countPage;
  97. if (countPage <= 1)
  98. {
  99. if (countPage == 1)
  100. {
  101. this.pageListBox.SelectedIndex = 0;
  102. }
  103. this.grid.Visibility = Visibility.Collapsed;
  104. }
  105. else
  106. {
  107. var k = DefeatPage;
  108. if (k > countPage) k = countPage;
  109. if (k < 1) k = 1;
  110. this.pageListBox.SelectedIndex = k - 1;
  111. //this.pageListBox.SelectedIndex = 0;
  112. this.grid.Visibility = Visibility.Visible;
  113. //this.countPageText.Text = $"共{countPage}页";
  114. this.countPageText.Text = $"{KeyToStringConvert.GetLanguageStringByKey("C0275")} {countPage} {KeyToStringConvert.GetLanguageStringByKey("C0276")}";
  115. }
  116. }
  117. private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  118. {
  119. if (!(sender is ListBox listBox))
  120. {
  121. return;
  122. }
  123. if (listBox.SelectedIndex == -1)
  124. {
  125. return;
  126. }
  127. if (listBox.SelectedIndex == 0 || listBox.SelectedIndex == (pageListBox.Items.Count - 1))
  128. {
  129. listBox.ScrollIntoView(listBox.Items[listBox.SelectedIndex]);
  130. }
  131. else
  132. {
  133. var scr = listBox.Template.FindName("scroll", listBox) as ScrollViewer;
  134. if (scr != null)
  135. {
  136. var k = listBox.SelectedIndex - 1;
  137. if (k < 0)
  138. {
  139. k = 0;
  140. }
  141. var aa = scr.HorizontalOffset;
  142. scr.ScrollToHorizontalOffset(k);
  143. }
  144. else
  145. {
  146. listBox.ScrollIntoView(listBox.Items[listBox.SelectedIndex]);
  147. }
  148. }
  149. PageChangedEvent?.Invoke((listBox.SelectedIndex + 1));
  150. }
  151. private void FirstPage_Click(object sender, RoutedEventArgs e)
  152. {
  153. pageListBox.SelectedIndex = 0;
  154. }
  155. private void PreviousPage_Click(object sender, RoutedEventArgs e)
  156. {
  157. var newIndex = (int)pageListBox.SelectedIndex - 1;
  158. if (newIndex >= 0)
  159. {
  160. pageListBox.SelectedIndex = newIndex;
  161. }
  162. }
  163. private void NextPage_Click(object sender, RoutedEventArgs e)
  164. {
  165. var newIndex = (int)pageListBox.SelectedIndex + 1;
  166. if (newIndex <= (pageListBox.Items.Count - 1))
  167. {
  168. pageListBox.SelectedIndex = newIndex;
  169. }
  170. }
  171. private void LastPage_Click(object sender, RoutedEventArgs e)
  172. {
  173. pageListBox.SelectedIndex = pageListBox.Items.Count - 1;
  174. }
  175. private void JumpPage_Click(object sender, RoutedEventArgs e)
  176. {
  177. string newPageString = this.jumpPage_TextBox.Text.Trim();
  178. if (string.IsNullOrEmpty(newPageString)) return;
  179. if (int.TryParse(newPageString, out int newPageInt))
  180. {
  181. if (newPageInt >= 1 && newPageInt <= pageListBox.Items.Count)
  182. {
  183. pageListBox.SelectedIndex = (newPageInt - 1);
  184. }
  185. }
  186. }
  187. }
  188. }