using ivf_tl_Operate.Converts; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ivf_tl_Operate.CustomUserControls { /// /// CustomControlPagination.xaml 的交互逻辑 /// public partial class CustomControlPagination : UserControl { public event Action PageChangedEvent; public CustomControlPagination() { InitializeComponent(); } /// /// 总页数 /// private int countPage = 0; public int DefeatPage { get { return (int)GetValue(DefeatPageProperty); } set { SetValue(DefeatPageProperty, value); } } // Using a DependencyProperty as the backing store for DefeatPage. This enables animation, styling, binding, etc... public static readonly DependencyProperty DefeatPageProperty = DependencyProperty.Register("DefeatPage", typeof(int), typeof(CustomControlPagination), new PropertyMetadata(1)); /// /// 总条目数量 /// public int CountNum { get { return (int)GetValue(CountNumProperty); } set { SetValue(CountNumProperty, value); } } // Using a DependencyProperty as the backing store for CountNum. This enables animation, styling, binding, etc... public static readonly DependencyProperty CountNumProperty = DependencyProperty.Register("CountNum", typeof(int), typeof(CustomControlPagination), new PropertyMetadata(-1, new PropertyChangedCallback(CountNumPropertyChangedCallback))); /// /// 每一页数量 /// public int PageNum { get { return (int)GetValue(PageNumProperty); } set { SetValue(PageNumProperty, value); } } // Using a DependencyProperty as the backing store for PageNum. This enables animation, styling, binding, etc... public static readonly DependencyProperty PageNumProperty = DependencyProperty.Register("PageNum", typeof(int), typeof(CustomControlPagination), new PropertyMetadata(0, new PropertyChangedCallback(PageNumPropertyChangedCallback))); private static void PageNumPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!(d is CustomControlPagination source)) return; int currentPageNum = (int)e.NewValue; source.WindowInit(currentPageNum, source.CountNum); } private static void CountNumPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!(d is CustomControlPagination source)) return; int max = (int)e.NewValue; source.WindowInit(source.PageNum, max); } private void WindowInit(int pageSize, int dataCount) { if (pageSize == -1 || dataCount == -1) return; if (dataCount == 0 || pageSize == 0) { PageChangedEvent?.Invoke(1); this.grid.Visibility = Visibility.Collapsed; return; } int countPage = dataCount / pageSize; if (dataCount % pageSize != 0) { countPage++; } ObservableCollection _items = new ObservableCollection(); for (int i = 1; i <= countPage; i++) { _items.Add(i); } this.pageListBox.SelectedIndex = -1; this.pageListBox.ItemsSource = _items; this.countPage = countPage; if (countPage <= 1) { if (countPage == 1) { this.pageListBox.SelectedIndex = 0; } this.grid.Visibility = Visibility.Collapsed; } else { var k = DefeatPage; if (k > countPage) k = countPage; if (k < 1) k = 1; this.pageListBox.SelectedIndex = k - 1; //this.pageListBox.SelectedIndex = 0; this.grid.Visibility = Visibility.Visible; //this.countPageText.Text = $"共{countPage}页"; this.countPageText.Text = $"{KeyToStringConvert.GetLanguageStringByKey("C0275")} {countPage} {KeyToStringConvert.GetLanguageStringByKey("C0276")}"; } } private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (!(sender is ListBox listBox)) { return; } if (listBox.SelectedIndex == -1) { return; } if (listBox.SelectedIndex == 0 || listBox.SelectedIndex == (pageListBox.Items.Count - 1)) { listBox.ScrollIntoView(listBox.Items[listBox.SelectedIndex]); } else { var scr = listBox.Template.FindName("scroll", listBox) as ScrollViewer; if (scr != null) { var k = listBox.SelectedIndex - 1; if (k < 0) { k = 0; } var aa = scr.HorizontalOffset; scr.ScrollToHorizontalOffset(k); } else { listBox.ScrollIntoView(listBox.Items[listBox.SelectedIndex]); } } PageChangedEvent?.Invoke((listBox.SelectedIndex + 1)); } private void FirstPage_Click(object sender, RoutedEventArgs e) { pageListBox.SelectedIndex = 0; } private void PreviousPage_Click(object sender, RoutedEventArgs e) { var newIndex = (int)pageListBox.SelectedIndex - 1; if (newIndex >= 0) { pageListBox.SelectedIndex = newIndex; } } private void NextPage_Click(object sender, RoutedEventArgs e) { var newIndex = (int)pageListBox.SelectedIndex + 1; if (newIndex <= (pageListBox.Items.Count - 1)) { pageListBox.SelectedIndex = newIndex; } } private void LastPage_Click(object sender, RoutedEventArgs e) { pageListBox.SelectedIndex = pageListBox.Items.Count - 1; } private void JumpPage_Click(object sender, RoutedEventArgs e) { string newPageString = this.jumpPage_TextBox.Text.Trim(); if (string.IsNullOrEmpty(newPageString)) return; if (int.TryParse(newPageString, out int newPageInt)) { if (newPageInt >= 1 && newPageInt <= pageListBox.Items.Count) { pageListBox.SelectedIndex = (newPageInt - 1); } } } } }