IntegerTextBox.xaml.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using MECF.Framework.Common.Equipment;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace MECF.Framework.UI.Core.Control
  18. {
  19. /// <summary>
  20. /// NumbericUpDown.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class IntegerTextBox : UserControl
  23. {
  24. #region 属性
  25. public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
  26. "Value", typeof(int), typeof(IntegerTextBox ), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));
  27. /// <summary>
  28. /// 数值
  29. /// </summary>
  30. public int Value
  31. {
  32. get
  33. {
  34. return (int)this.GetValue(ValueProperty);
  35. }
  36. set
  37. {
  38. this.SetValue(ValueProperty, value);
  39. }
  40. }
  41. public static readonly DependencyProperty TextboxNameProperty = DependencyProperty.Register(
  42. "TextboxName", typeof(string), typeof(IntegerTextBox ), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  43. /// <summary>
  44. /// 名称
  45. /// </summary>
  46. public string TextboxName
  47. {
  48. get
  49. {
  50. return (string)this.GetValue(TextboxNameProperty);
  51. }
  52. set
  53. {
  54. this.SetValue(TextboxNameProperty, value);
  55. }
  56. }
  57. public static readonly DependencyProperty KeyOperationProperty = DependencyProperty.Register("KeyOperation", typeof(ICommand), typeof(IntegerTextBox ));
  58. public ICommand KeyOperation
  59. {
  60. get
  61. {
  62. return (ICommand)this.GetValue(KeyOperationProperty);
  63. }
  64. set
  65. {
  66. this.SetValue(KeyOperationProperty, value);
  67. }
  68. }
  69. #endregion
  70. /// <summary>
  71. /// 构造函数
  72. /// </summary>
  73. public IntegerTextBox ()
  74. {
  75. InitializeComponent();
  76. }
  77. private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
  78. {
  79. e.Handled = !IsTextAllowed(e.Text, ((TextBox)sender).Text);
  80. }
  81. private bool IsTextAllowed(string key, string text)
  82. {
  83. Regex regex = new Regex("[^0-9]+"); //regex that matches disallowed text
  84. return !regex.IsMatch(key);
  85. }
  86. private void txtInput_KeyDown(object sender, KeyEventArgs e)
  87. {
  88. if(e.Key == Key.Enter)
  89. {
  90. if(KeyOperation!=null)
  91. {
  92. KeyOperation.Execute(new object[] { TextboxName, txtInput.Text});
  93. }
  94. }
  95. }
  96. }
  97. }