using System; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; namespace ivf_tl_Manage.UserControls { /// /// PaginationUserControl.xaml 的交互逻辑 /// public partial class PaginationUserControl : UserControl { public event Action PageChangedEvent; public event Action ClickPageEvent; private int scrNum = 2; public PaginationUserControl() { InitializeComponent(); } public Thickness HeadMargin { get { return (Thickness)GetValue(HeadMarginProperty); } set { SetValue(HeadMarginProperty, value); } } public static readonly DependencyProperty HeadMarginProperty = DependencyProperty.Register("HeadMargin", typeof(Thickness), typeof(PaginationUserControl), new PropertyMetadata(new Thickness(40, 0, 0, 0))); public Thickness LastMargin { get { return (Thickness)GetValue(LastMarginProperty); } set { SetValue(LastMarginProperty, value); } } public static readonly DependencyProperty LastMarginProperty = DependencyProperty.Register("LastMargin", typeof(Thickness), typeof(PaginationUserControl), new PropertyMetadata(new Thickness(0, 0, 40, 0))); private void Ok_Click(object sender, RoutedEventArgs e) { } 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(PaginationUserControl), new PropertyMetadata(1)); private static void dsf(DependencyObject d, DependencyPropertyChangedEventArgs e) { string ss = ""; } public int PageSize { get { return (int)GetValue(PageSizeProperty); } set { SetValue(PageSizeProperty, value); } } public static readonly DependencyProperty PageSizeProperty = DependencyProperty.Register("PageSize", typeof(int), typeof(PaginationUserControl), new PropertyMetadata(-1, new PropertyChangedCallback(PageSizePropertyChangedCallback))); private static void PageSizePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!(d is PaginationUserControl source)) return; int newPageSize = (int)e.NewValue; source.WindowInit(newPageSize, source.DataCount); } public int DataCount { get { return (int)GetValue(DataCountProperty); } set { SetValue(DataCountProperty, value); } } public static readonly DependencyProperty DataCountProperty = DependencyProperty.Register("DataCount", typeof(int), typeof(PaginationUserControl), new PropertyMetadata(-1, new PropertyChangedCallback(DataCountPropertyChangedCallback))); private static void DataCountPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (!(d is PaginationUserControl source)) return; int newDataCount = (int)e.NewValue; source.WindowInit(source.PageSize, newDataCount); } private void WindowInit(int newPageSize, int newDataCount) { if (newPageSize < 0 || newDataCount < 0) return; if (newPageSize == 0 || newDataCount == 0) { PageCount = 1; } else { int countPage = newDataCount / newPageSize; if (newDataCount % newPageSize != 0) { countPage++; } PageCount = countPage; } if (PageCount < 1) PageCount = 1; ObservableCollection _items = new ObservableCollection(); for (int i = 1; i <= PageCount; i++) { _items.Add(i); } this.pageListBox.SelectedIndex = -1; this.pageListBox.ItemsSource = _items; var k = DefeatPage; if (k > PageCount) k = PageCount; if (k < 1) k = 1; test = true; this.pageListBox.SelectedIndex = k - 1; test = false; } public bool test = false; public int PageCount { get { return (int)GetValue(PageCountProperty); } set { SetValue(PageCountProperty, value); } } public static readonly DependencyProperty PageCountProperty = DependencyProperty.Register("PageCount", typeof(int), typeof(PaginationUserControl), new PropertyMetadata(-1)); private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (!(sender is ListBox listBox)) return; if (listBox.SelectedIndex == -1) return; if (listBox.SelectedIndex < scrNum || listBox.Items.Count - listBox.SelectedIndex - 1 < scrNum) { listBox.ScrollIntoView(listBox.Items[listBox.SelectedIndex]); if (!test) ClickPageEvent?.Invoke(); PageChangedEvent?.Invoke(this, (listBox.SelectedIndex + 1)); return; } var scr = listBox.Template.FindName("scroll", listBox) as ScrollViewer; if (scr == null) { listBox.ScrollIntoView(listBox.Items[listBox.SelectedIndex]); if (!test) ClickPageEvent?.Invoke(); PageChangedEvent?.Invoke(this, (listBox.SelectedIndex + 1)); return; } scr.ScrollToHorizontalOffset(listBox.SelectedIndex - scrNum); if (!test) ClickPageEvent?.Invoke(); PageChangedEvent?.Invoke(this, (listBox.SelectedIndex + 1)); } private void JumpPage_Click(object sender, RoutedEventArgs e) { if (int.TryParse(this.jumpPage_TextBox.Text.Trim(), out int newPage) && newPage >= 1 && newPage <= PageCount) { ClickPageEvent?.Invoke(); this.pageListBox.SelectedIndex = newPage - 1; return; } return; } private void PageFirst_Click(object sender, RoutedEventArgs e) { if (!this.pageListBox.HasItems) return; if (this.pageListBox.SelectedIndex != 0) { ClickPageEvent?.Invoke(); this.pageListBox.SelectedIndex = 0; } } private void PagePre_Click(object sender, RoutedEventArgs e) { if (!this.pageListBox.HasItems) return; if (this.pageListBox.SelectedIndex > 0) { ClickPageEvent?.Invoke(); this.pageListBox.SelectedIndex--; } } private void PageNext_Click(object sender, RoutedEventArgs e) { if (!this.pageListBox.HasItems) return; if (this.pageListBox.SelectedIndex < (PageCount - 1)) { ClickPageEvent?.Invoke(); this.pageListBox.SelectedIndex++; } } private void PageLast_Click(object sender, RoutedEventArgs e) { if (!this.pageListBox.HasItems) return; if (this.pageListBox.SelectedIndex != (PageCount - 1)) { ClickPageEvent?.Invoke(); this.pageListBox.SelectedIndex = PageCount - 1; } } public string PageInfoString { get { return (string)GetValue(PageInfoStringProperty); } set { SetValue(PageInfoStringProperty, value); } } public static readonly DependencyProperty PageInfoStringProperty = DependencyProperty.Register("PageInfoString", typeof(string), typeof(PaginationUserControl), new PropertyMetadata("张图片")); public string PageUnit { get { return (string)GetValue(PageUnitProperty); } set { SetValue(PageUnitProperty, value); } } public static readonly DependencyProperty PageUnitProperty = DependencyProperty.Register("PageUnit", typeof(string), typeof(PaginationUserControl), new PropertyMetadata("张")); } }