From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点
---
HMI.UI/Controls/NumberInput.cs | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 102 insertions(+), 0 deletions(-)
diff --git a/HMI.UI/Controls/NumberInput.cs b/HMI.UI/Controls/NumberInput.cs
new file mode 100644
index 0000000..01d4234
--- /dev/null
+++ b/HMI.UI/Controls/NumberInput.cs
@@ -0,0 +1,102 @@
+锘縰sing System.Text.RegularExpressions;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Input;
+
+namespace HMI.UI
+{
+ public class NumberInput : TextBox
+ {
+ public NumberInput()
+ {
+ DataObject.AddPastingHandler(this, OnPasting);
+
+ // 绂佺敤杈撳叆娉�
+ InputMethod.SetIsInputMethodEnabled(this, false);
+
+ // 璁剧疆杈撳叆娉曟ā寮忎负瀛楁瘝
+ InputScope inputScope = new InputScope();
+ InputScopeName inputScopeName = new InputScopeName();
+ inputScopeName.NameValue = InputScopeNameValue.Number;
+ inputScope.Names.Add(inputScopeName);
+ this.InputScope = inputScope;
+ }
+
+ static NumberInput()
+ {
+ DefaultStyleKeyProperty.OverrideMetadata(typeof(NumberInput),
+ new FrameworkPropertyMetadata(typeof(NumberInput)));
+ }
+
+ public override void OnApplyTemplate()
+ {
+ base.OnApplyTemplate();
+ }
+
+ protected override void OnPreviewTextInput(TextCompositionEventArgs e)
+ {
+ // 鍏佽杈撳叆鏁板瓧銆佽礋鍙峰拰灏忔暟鐐�
+ Regex regex = new Regex(@"^-?\d*\.?\d*$");
+ string segment = e.Text == "." ? ".0" : e.Text;
+ string newText = this.Text.Insert(this.CaretIndex, segment);
+
+ // 妫�鏌ユ槸鍚﹀凡缁忔湁璐熷彿锛屽鏋滄湁鍒欎笉鍏佽鍐嶈緭鍏ヨ礋鍙�
+ if (e.Text == "-" && this.Text.Contains("-"))
+ {
+ e.Handled = true;
+ return;
+ }
+
+ // 妫�鏌ユ槸鍚﹀凡缁忔湁灏忔暟鐐癸紝濡傛灉鏈夊垯涓嶅厑璁稿啀杈撳叆灏忔暟鐐�
+ if (e.Text == "." && this.Text.Contains("."))
+ {
+ e.Handled = true;
+ return;
+ }
+ //else if (e.Text == ".") {
+ // e.Text = ".0";
+ //}
+ //regex = new Regex(@"^-?\d*\.?\d*$|^-?\.\d*$|^-?\d+\.$");
+ // 楠岃瘉瀹屾暣鐨勬柊鏂囨湰鏄惁绗﹀悎鏁板瓧鏍煎紡
+ if (!regex.IsMatch(newText))
+ {
+ e.Handled = true;
+ return;
+ }
+
+ base.OnPreviewTextInput(e);
+ }
+
+ protected override void OnPreviewKeyDown(KeyEventArgs e)
+ {
+ // 鍏佽浣跨敤閫�鏍奸敭鍜屽垹闄ら敭
+ if (e.Key == Key.Back || e.Key == Key.Delete)
+ {
+ base.OnPreviewKeyDown(e);
+ return;
+ }
+
+ // 鍏佽澶嶅埗绮樿创绛夊揩鎹烽敭
+ if (Keyboard.Modifiers == ModifierKeys.Control &&
+ (e.Key == Key.C || e.Key == Key.V || e.Key == Key.X))
+ {
+ base.OnPreviewKeyDown(e);
+ return;
+ }
+ }
+
+ private void OnPasting(object sender, DataObjectPastingEventArgs e)
+ {
+ if (e.DataObject.GetDataPresent(typeof(string)))
+ {
+ string text = (string)e.DataObject.GetData(typeof(string));
+ Regex regex = new Regex(@"^-?\d*\.?\d*$");
+
+ if (!regex.IsMatch(text))
+ {
+ e.CancelCommand();
+ }
+ }
+ }
+ }
+}
--
Gitblit v1.9.1