alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
 
namespace UILib
{
    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();
                }
            }
        }
    }
}