PaginationList.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using FurnaceUI.Views.Recipes;
  7. namespace FurnaceUI.Controls
  8. {
  9. partial class PaginationList : UserControl, INotifyPropertyChanged
  10. {
  11. #region 属性
  12. public List<BoatWaferItem> SubItems { private get; set; }
  13. public List<BoatWaferItem> Items
  14. {
  15. get { return (List<BoatWaferItem>)GetValue(ItemsProperty); }
  16. set { SetValue(ItemsProperty, value); }
  17. }
  18. public static readonly DependencyProperty ItemsProperty =
  19. DependencyProperty.Register("Items", typeof(List<BoatWaferItem>), typeof(PaginationList),new PropertyMetadata(null, new PropertyChangedCallback(RenderUpdateByPropertyChanged)));
  20. public int CurrentItemIndex
  21. {
  22. get { return (int)GetValue(CurrentItemIndexProperty); }
  23. set { SetValue(CurrentItemIndexProperty, value); }
  24. }
  25. public static readonly DependencyProperty CurrentItemIndexProperty =
  26. DependencyProperty.Register("CurrentItemIndex", typeof(int), typeof(PaginationList), new PropertyMetadata(-1, new PropertyChangedCallback(RenderUpdateByPropertyChanged)));
  27. #endregion
  28. #region 页面属性
  29. public int PageIndex { get; set; } = 1;
  30. public int PageCount { get; set; }
  31. public int RecordCount { get; set; }
  32. public int PageSize
  33. {
  34. get { return (int)GetValue(PageSizeProperty); }
  35. set { SetValue(PageSizeProperty, value); }
  36. }
  37. public static readonly DependencyProperty PageSizeProperty =
  38. DependencyProperty.Register("PageSize", typeof(int), typeof(PaginationList), new PropertyMetadata(1, new PropertyChangedCallback(RenderUpdateByPropertyChanged)));
  39. public bool IsListEnable
  40. {
  41. get { return (bool)GetValue(IsListEnableProperty); }
  42. set { SetValue(IsListEnableProperty, value); }
  43. }
  44. public static readonly DependencyProperty IsListEnableProperty =
  45. DependencyProperty.Register("IsListEnable", typeof(bool), typeof(PaginationList), new PropertyMetadata(true, new PropertyChangedCallback(RenderUpdateByPropertyChanged)));
  46. public event PropertyChangedEventHandler PropertyChanged;
  47. #endregion
  48. #region 翻页命令
  49. public void NextPageCommand()
  50. {
  51. if (PageIndex < PageCount)
  52. {
  53. PageIndex = PageIndex + 1;
  54. TakePage();
  55. }
  56. }
  57. public void PreviousPageCommand()
  58. {
  59. if (PageIndex > 1)
  60. {
  61. PageIndex = PageIndex - 1; ;
  62. TakePage();
  63. }
  64. }
  65. public void HomePageCommand()
  66. {
  67. PageIndex = 1;
  68. TakePage();
  69. }
  70. public void TailPageCommand()
  71. {
  72. PageIndex = PageCount;
  73. TakePage();
  74. }
  75. #endregion
  76. public PaginationList()
  77. {
  78. InitializeComponent();
  79. }
  80. /// <summary>
  81. /// 当属性刷新的时候,进行强制更新绘图
  82. /// </summary>
  83. /// <param name="dependency"></param>
  84. /// <param name="e"></param>
  85. public static void RenderUpdateByPropertyChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs e)
  86. {
  87. if (dependency is PaginationList hslThermometer)
  88. {
  89. hslThermometer.TakePage();
  90. }
  91. }
  92. private void TakePage()
  93. {
  94. if (PageSize <= 0 || Items == null || Items.Count<=0) return;
  95. PageCount = Items.Count / PageSize;
  96. RecordCount = Items.Count % PageSize;
  97. if (RecordCount > 0)
  98. PageCount += 1;
  99. if (PageIndex == PageCount)//最后一页
  100. {
  101. RecordCount = RecordCount == 0 ? PageSize : RecordCount;
  102. SubItems = Items.GetRange((PageIndex - 1) * PageSize, RecordCount);
  103. }
  104. else
  105. {
  106. SubItems = Items.GetRange((PageIndex - 1) * PageSize, PageSize);
  107. }
  108. SortSubItems();
  109. list.ItemsSource = SubItems;
  110. list.IsEnabled = IsListEnable;
  111. }
  112. private void SortSubItems()
  113. {
  114. BoatWaferItem[] TempList = new BoatWaferItem[SubItems.Count];
  115. var length = TempList.Length % 2 == 0 ? TempList.Length : TempList.Length + 1;
  116. for (int i = 0; i < TempList.Length; i++)
  117. {
  118. if(i%2==0)
  119. TempList[i] = SubItems[i/2];
  120. else
  121. TempList[i] = SubItems[i/2 + length / 2];
  122. }
  123. SubItems =new List<BoatWaferItem>(TempList);
  124. }
  125. private void Button_Click(object sender, RoutedEventArgs e)
  126. {
  127. Button btn = sender as Button;
  128. switch (btn.Tag)
  129. {
  130. case "HomePage":
  131. HomePageCommand();
  132. break;
  133. case "PrePage":
  134. PreviousPageCommand();
  135. break;
  136. case "NextPage":
  137. NextPageCommand();
  138. break;
  139. case "TailPage":
  140. TailPageCommand();
  141. break;
  142. }
  143. }
  144. private void Button_Click_1(object sender, RoutedEventArgs e)
  145. {
  146. Button btn = sender as Button;
  147. CurrentItemIndex = (int)btn.Tag;
  148. }
  149. }
  150. }