PaginationControl.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace PDF_Master.CustomControl
  16. {
  17. /// <summary>
  18. /// PaginationControl.xaml 的交互逻辑
  19. /// </summary>
  20. public partial class PaginationControl : UserControl
  21. {
  22. public PaginationControl()
  23. {
  24. InitializeComponent();
  25. }
  26. public int CurrentPage
  27. {
  28. get { return (int)GetValue(CurrentPageProperty); }
  29. set { SetValue(CurrentPageProperty, value); }
  30. }
  31. // Using a DependencyProperty as the backing store for CurrentPage. This enables animation, styling, binding, etc...
  32. public static readonly DependencyProperty CurrentPageProperty =
  33. DependencyProperty.Register("CurrentPage", typeof(int), typeof(PaginationControl), new PropertyMetadata(0));
  34. public int PageCount
  35. {
  36. get { return (int)GetValue(PageCountProperty); }
  37. set { SetValue(PageCountProperty, value); }
  38. }
  39. // Using a DependencyProperty as the backing store for PageCount. This enables animation, styling, binding, etc...
  40. public static readonly DependencyProperty PageCountProperty =
  41. DependencyProperty.Register("PageCount", typeof(int), typeof(PaginationControl), new PropertyMetadata(0));
  42. public string InputString
  43. {
  44. get { return (string)GetValue(InputStringProperty); }
  45. set { SetValue(InputStringProperty, value); }
  46. }
  47. // Using a DependencyProperty as the backing store for InputString. This enables animation, styling, binding, etc...
  48. public static readonly DependencyProperty InputStringProperty =
  49. DependencyProperty.Register("InputString", typeof(string), typeof(PaginationControl), new PropertyMetadata(""));
  50. private void CheckInput()
  51. {
  52. int page = CurrentPage;
  53. bool result = int.TryParse(InputString,out page);
  54. if(result&&(page <= PageCount)&&page>=1)
  55. {
  56. CurrentPage = page;
  57. }
  58. else
  59. {
  60. InputString = CurrentPage.ToString();
  61. }
  62. }
  63. private async void TblIndex_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  64. {
  65. if(e.ClickCount==2)
  66. {
  67. TblIndex.Visibility = Visibility.Collapsed;
  68. TxtInput.Visibility = Visibility.Visible;
  69. await Task.Delay(20);
  70. TxtInput.SelectAll();
  71. TxtInput.Focus();
  72. InputString = CurrentPage.ToString();
  73. }
  74. }
  75. private void TxtInput_LostFocus(object sender, RoutedEventArgs e)
  76. {
  77. CheckInput();
  78. TblIndex.Visibility = Visibility.Visible;
  79. TxtInput.Visibility = Visibility.Collapsed;
  80. }
  81. private void TxtInput_PreviewKeyDown(object sender, KeyEventArgs e)
  82. {
  83. if (e.Key == Key.Enter)
  84. {
  85. CheckInput();
  86. }
  87. }
  88. }
  89. }