NumbericTextBox.xaml.cs 4.1 KB

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