| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using System;
- using System.Collections.ObjectModel;
- using System.Windows;
- using System.Windows.Controls;
- namespace ivf_tl_Manage.UserControls
- {
- /// <summary>
- /// PaginationUserControl.xaml 的交互逻辑
- /// </summary>
- public partial class PaginationUserControl : UserControl
- {
- public event Action<PaginationUserControl, int> 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<int> _items = new ObservableCollection<int>();
- 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("张"));
- }
- }
|