PMCounterValueTextBox.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using SciChart.Core.Extensions;
  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. /// PMCounterValueTextBox.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class PMCounterValueTextBox : UserControl
  23. {
  24. public PMCounterValueTextBox()
  25. {
  26. InitializeComponent();
  27. }
  28. public static readonly DependencyProperty KeyOperationProperty = DependencyProperty.Register("KeyOperation", typeof(ICommand), typeof(PMCounterValueTextBox));
  29. public ICommand KeyOperation
  30. {
  31. get
  32. {
  33. return (ICommand)this.GetValue(KeyOperationProperty);
  34. }
  35. set
  36. {
  37. this.SetValue(KeyOperationProperty, value);
  38. }
  39. }
  40. public static readonly DependencyProperty PMCounterNodeNameProperty = DependencyProperty.Register(
  41. "PMCounterNodeName", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  42. /// <summary>
  43. /// PMCounter名称
  44. /// </summary>
  45. public string PMCounterNodeName
  46. {
  47. get
  48. {
  49. return (string)this.GetValue(PMCounterNodeNameProperty);
  50. }
  51. set
  52. {
  53. this.SetValue(PMCounterNodeNameProperty, value);
  54. }
  55. }
  56. public static readonly DependencyProperty CounterNodeNameProperty = DependencyProperty.Register(
  57. "CounterNodeName", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  58. /// <summary>
  59. /// PMcounter项目名称
  60. /// </summary>
  61. public string CounterNodeName
  62. {
  63. get
  64. {
  65. return (string)this.GetValue(CounterNodeNameProperty);
  66. }
  67. set
  68. {
  69. this.SetValue(CounterNodeNameProperty, value);
  70. }
  71. }
  72. public static readonly DependencyProperty CounterNodeItemIndexProperty = DependencyProperty.Register(
  73. "CounterNodeItemIndex", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  74. /// <summary>
  75. /// PMcounterItem Index
  76. /// </summary>
  77. public string CounterNodeItemIndex
  78. {
  79. get
  80. {
  81. return (string)this.GetValue(CounterNodeItemIndexProperty);
  82. }
  83. set
  84. {
  85. this.SetValue(CounterNodeItemIndexProperty, value);
  86. }
  87. }
  88. public static readonly DependencyProperty IsHeaderProperty = DependencyProperty.Register(
  89. "IsHeader", typeof(bool), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
  90. /// <summary>
  91. /// IsHeader
  92. /// </summary>
  93. public bool IsHeader
  94. {
  95. get
  96. {
  97. return (bool)this.GetValue(IsHeaderProperty);
  98. }
  99. set
  100. {
  101. this.SetValue(IsHeaderProperty, value);
  102. }
  103. }
  104. public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
  105. "Value", typeof(string), typeof(PMCounterValueTextBox), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
  106. /// <summary>
  107. /// 数值
  108. /// </summary>
  109. public string Value
  110. {
  111. get
  112. {
  113. return (string)this.GetValue(ValueProperty);
  114. }
  115. set
  116. {
  117. this.SetValue(ValueProperty, value);
  118. }
  119. }
  120. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  121. {
  122. if (e.Key == Key.Enter)
  123. {
  124. if (KeyOperation != null)
  125. {
  126. if (txtInput.Text.Trim().IsNullOrEmpty())
  127. {
  128. txtInput.Text = "0";
  129. }
  130. if (txtInput.Text.Trim().Last() == '.')
  131. {
  132. txtInput.Text = txtInput.Text.Trim().Substring(0, txtInput.Text.Trim().Length - 1);
  133. }
  134. Value = txtInput.Text;
  135. KeyOperation.Execute(new object[] { PMCounterNodeName, CounterNodeName, txtInput.Text });
  136. }
  137. }
  138. }
  139. private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
  140. {
  141. var textBox = (TextBox)sender;
  142. e.Handled = !IsTextAllowed(textBox.Text, e.Text[0]);
  143. }
  144. private bool IsTextAllowed(string input, char? newChar = null)
  145. {
  146. //regex that matches disallowed text
  147. //Regex regex = new Regex("[^0-9.]+");
  148. //return !regex.IsMatch(key);
  149. // 检查是否包含空格
  150. if ((newChar.HasValue && char.IsWhiteSpace(newChar.Value)) ||
  151. (!string.IsNullOrEmpty(input) && input.Any(char.IsWhiteSpace)))
  152. {
  153. return false;
  154. }
  155. // 允许空字符串(用于删除操作)
  156. if (string.IsNullOrEmpty(input) && newChar == null)
  157. return true;
  158. // 组合完整字符串
  159. string fullText = newChar.HasValue ? input + newChar : input;
  160. // 正则表达式验证(允许数字和单个小数点,禁止空格)
  161. return Regex.IsMatch(fullText, @"^-?\d*\.?\d*$") &&
  162. fullText.Count(c => c == '.') <= 1;
  163. }
  164. private void TextBox_LostFocus(object sender, RoutedEventArgs e)
  165. {
  166. TextBox textBox = sender as TextBox;
  167. if (textBox != null && string.IsNullOrEmpty(textBox.Text))
  168. {
  169. textBox.Text = Value;
  170. }
  171. if (Value != textBox.Text)
  172. {
  173. textBox.Text = Value;
  174. }
  175. }
  176. }
  177. }