PaginationList.xaml.cs 5.6 KB

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