using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using FurnaceUI.Views.Recipes; namespace FurnaceUI.Controls { partial class PaginationList : UserControl, INotifyPropertyChanged { #region 属性 public List SubItems { private get; set; } public List Items { get { return (List)GetValue(ItemsProperty); } set { SetValue(ItemsProperty, value); } } public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(List), typeof(PaginationList),new PropertyMetadata(null, new PropertyChangedCallback(RenderUpdateByPropertyChanged))); public int CurrentItemIndex { get { return (int)GetValue(CurrentItemIndexProperty); } set { SetValue(CurrentItemIndexProperty, value); } } public static readonly DependencyProperty CurrentItemIndexProperty = DependencyProperty.Register("CurrentItemIndex", typeof(int), typeof(PaginationList), new PropertyMetadata(-1, new PropertyChangedCallback(RenderUpdateByPropertyChanged))); #endregion #region 页面属性 public int PageIndex { get; set; } = 1; public int PageCount { get; set; } public int RecordCount { get; set; } public int PageSize { get { return (int)GetValue(PageSizeProperty); } set { SetValue(PageSizeProperty, value); } } public static readonly DependencyProperty PageSizeProperty = DependencyProperty.Register("PageSize", typeof(int), typeof(PaginationList), new PropertyMetadata(1, new PropertyChangedCallback(RenderUpdateByPropertyChanged))); public bool IsListEnable { get { return (bool)GetValue(IsListEnableProperty); } set { SetValue(IsListEnableProperty, value); } } public static readonly DependencyProperty IsListEnableProperty = DependencyProperty.Register("IsListEnable", typeof(bool), typeof(PaginationList), new PropertyMetadata(true, new PropertyChangedCallback(RenderUpdateByPropertyChanged))); public event PropertyChangedEventHandler PropertyChanged; #endregion #region 翻页命令 public void NextPageCommand() { if (PageIndex < PageCount) { PageIndex = PageIndex + 1; TakePage(); } } public void PreviousPageCommand() { if (PageIndex > 1) { PageIndex = PageIndex - 1; ; TakePage(); } } public void HomePageCommand() { PageIndex = 1; TakePage(); } public void TailPageCommand() { PageIndex = PageCount; TakePage(); } #endregion public PaginationList() { InitializeComponent(); } /// /// 当属性刷新的时候,进行强制更新绘图 /// /// /// public static void RenderUpdateByPropertyChanged(DependencyObject dependency, DependencyPropertyChangedEventArgs e) { if (dependency is PaginationList hslThermometer) { hslThermometer.TakePage(); } } private void TakePage() { if (PageSize <= 0 || Items == null || Items.Count<=0) return; PageCount = Items.Count / PageSize; RecordCount = Items.Count % PageSize; if (RecordCount > 0) PageCount += 1; if (PageIndex == PageCount)//最后一页 { RecordCount = RecordCount == 0 ? PageSize : RecordCount; SubItems = Items.GetRange((PageIndex - 1) * PageSize, RecordCount); } else { SubItems = Items.GetRange((PageIndex - 1) * PageSize, PageSize); } SortSubItems(); list.ItemsSource = SubItems; list.IsEnabled = IsListEnable; } private void SortSubItems() { BoatWaferItem[] TempList = new BoatWaferItem[SubItems.Count]; var length = TempList.Length % 2 == 0 ? TempList.Length : TempList.Length + 1; for (int i = 0; i < TempList.Length; i++) { if(i%2==0) TempList[i] = SubItems[i/2]; else TempList[i] = SubItems[i/2 + length / 2]; } SubItems =new List(TempList); } private void Button_Click(object sender, RoutedEventArgs e) { Button btn = sender as Button; switch (btn.Tag) { case "HomePage": HomePageCommand(); break; case "PrePage": PreviousPageCommand(); break; case "NextPage": NextPageCommand(); break; case "TailPage": TailPageCommand(); break; } } private void Button_Click_1(object sender, RoutedEventArgs e) { Button btn = sender as Button; CurrentItemIndex = (int)btn.Tag; } } }