using System.Windows.Controls; using System.Windows.Input; namespace MuchInfo.Chart.FormulaEdit { public class NumberTextBox : TextBox { protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (!IsNumber((byte)e.Key)) { e.Handled = true; } } protected override void OnTextChanged(TextChangedEventArgs e) { if (!string.IsNullOrEmpty(this.Text)) { double number = 0d; if (!double.TryParse(this.Text, out number)) { this.Text = string.Empty; } } base.OnTextChanged(e); } /// /// 输入 是否为数字 /// /// /// private bool IsNumber(byte key) { // 数字 if (key >= 34 && key <= 43) return true; // 小数字键盘 if (key >= 74 && key <= 83) return true; // Backspace键 if (key == 2) return true; return false; } } }