|
@@ -125,29 +125,53 @@ namespace MECF.Framework.UI.Core.Control
|
|
|
{
|
|
|
if (KeyOperation != null)
|
|
|
{
|
|
|
- if (txtInput.Text.IsNullOrEmpty())
|
|
|
+ if (txtInput.Text.Trim().IsNullOrEmpty())
|
|
|
{
|
|
|
txtInput.Text = "0";
|
|
|
}
|
|
|
+ if (txtInput.Text.Trim().Last() == '.')
|
|
|
+ {
|
|
|
+ txtInput.Text = txtInput.Text.Trim().Substring(0, txtInput.Text.Trim().Length - 1);
|
|
|
+ }
|
|
|
Value = txtInput.Text;
|
|
|
- KeyOperation.Execute(new object[] { PMCounterNodeName, CounterNodeName , txtInput.Text });
|
|
|
+ KeyOperation.Execute(new object[] { PMCounterNodeName, CounterNodeName, txtInput.Text });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
|
|
|
{
|
|
|
- e.Handled = !IsTextAllowed(e.Text, ((TextBox)sender).Text);
|
|
|
+ var textBox = (TextBox)sender;
|
|
|
+ e.Handled = !IsTextAllowed(textBox.Text, e.Text[0]);
|
|
|
}
|
|
|
|
|
|
- private bool IsTextAllowed(string key, string text)
|
|
|
+ private bool IsTextAllowed(string input, char? newChar = null)
|
|
|
{
|
|
|
- Regex regex = new Regex("[^0-9.]+"); //regex that matches disallowed text
|
|
|
- return !regex.IsMatch(key);
|
|
|
+ //regex that matches disallowed text
|
|
|
+ //Regex regex = new Regex("[^0-9.]+");
|
|
|
+ //return !regex.IsMatch(key);
|
|
|
+ // 检查是否包含空格
|
|
|
+ if ((newChar.HasValue && char.IsWhiteSpace(newChar.Value)) ||
|
|
|
+ (!string.IsNullOrEmpty(input) && input.Any(char.IsWhiteSpace)))
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 允许空字符串(用于删除操作)
|
|
|
+ if (string.IsNullOrEmpty(input) && newChar == null)
|
|
|
+ return true;
|
|
|
+
|
|
|
+ // 组合完整字符串
|
|
|
+ string fullText = newChar.HasValue ? input + newChar : input;
|
|
|
+
|
|
|
+ // 正则表达式验证(允许数字和单个小数点,禁止空格)
|
|
|
+ return Regex.IsMatch(fullText, @"^-?\d*\.?\d*$") &&
|
|
|
+ fullText.Count(c => c == '.') <= 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
TextBox textBox = sender as TextBox;
|