NumericUpDown.xaml.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace PDF_Office.CustomControl
  17. {
  18. /// <summary>
  19. /// NumericUpDown.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class NumericUpDown : UserControl
  22. {
  23. public int Num
  24. {
  25. get
  26. {
  27. int value = 0;
  28. this.Dispatcher.Invoke(new Action(() =>
  29. value = Convert.ToInt32(this.TextBox_Num.Text.Trim())
  30. ));
  31. return value;
  32. }
  33. set
  34. {
  35. this.Dispatcher.Invoke(new Action(() =>
  36. {
  37. this.TextBox_Num.Text = value.ToString();
  38. }));
  39. }
  40. }
  41. public NumericUpDown()
  42. {
  43. InitializeComponent();
  44. }
  45. private void Button_Add_Click(object sender, RoutedEventArgs e)
  46. {
  47. int num = int.Parse(this.TextBox_Num.Text.Trim());
  48. if (num > 0)
  49. {
  50. this.TextBox_Num.Text = (num + 1).ToString();
  51. Text = this.TextBox_Num.Text;
  52. }
  53. }
  54. private void Button_Sub_Click(object sender, RoutedEventArgs e)
  55. {
  56. int num = int.Parse(this.TextBox_Num.Text.Trim());
  57. if (num > 0)
  58. {
  59. if ((num - 1) == 0)
  60. return;
  61. this.TextBox_Num.Text = (num - 1).ToString();
  62. Text = this.TextBox_Num.Text;
  63. }
  64. }
  65. public string Text
  66. {
  67. get { return (string)GetValue(TextProperty); }
  68. set { SetValue(TextProperty, value); }
  69. }
  70. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  71. public static readonly DependencyProperty TextProperty =
  72. DependencyProperty.Register("Text", typeof(string), typeof(NumericUpDown), new PropertyMetadata(""));
  73. private void TextBox_Num_TextChanged(object sender, TextChangedEventArgs e)
  74. {
  75. Text = this.TextBox_Num.Text;
  76. }
  77. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  78. {
  79. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  80. }
  81. }
  82. }