| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- 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
- {
- /// <summary>
- /// CustomControlPagination.xaml 的交互逻辑
- /// </summary>
- public partial class CustomControlPagination : UserControl
- {
- public event Action<int> PageChangedEvent;
- public CustomControlPagination()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 总页数
- /// </summary>
- 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));
- /// <summary>
- /// 总条目数量
- /// </summary>
- 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)));
- /// <summary>
- /// 每一页数量
- /// </summary>
- 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<int> _items = new ObservableCollection<int>();
- 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);
- }
- }
- }
- }
- }
|