NumericUpDown.xaml.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if (this.TextBox_Num.Text == "" || int.Parse(this.TextBox_Num.Text) < 1)
  76. {
  77. this.TextBox_Num.Text = "1";
  78. }
  79. Text = this.TextBox_Num.Text;
  80. }
  81. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  82. {
  83. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  84. }
  85. }
  86. }