using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; using System.Windows; using System.Windows.Input; namespace MyWPFCustomControls { public class CustomTextBox : TextBox { public CustomTextBox() { } static CustomTextBox() { } public CustomTextBoxType CustomTextBoxType { get { return (CustomTextBoxType)GetValue(CustomTextBoxTypeProperty); } set { SetValue(CustomTextBoxTypeProperty, value); } } // Using a DependencyProperty as the backing store for CustomTextBoxType. This enables animation, styling, binding, etc... public static readonly DependencyProperty CustomTextBoxTypeProperty = DependencyProperty.Register("CustomTextBoxType", typeof(CustomTextBoxType), typeof(CustomTextBox), new UIPropertyMetadata(CustomTextBoxType.None, new PropertyChangedCallback(OnCustomTextBoxTypeChanged))); private static void OnCustomTextBoxTypeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { var tb = obj as CustomTextBox; } public override void OnApplyTemplate() { base.OnApplyTemplate(); } protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e) { base.OnPreviewTextInput(e); } protected override void OnTextChanged(TextChangedEventArgs e) { base.OnTextChanged(e); //屏蔽中文输入和非法字符粘贴输入 TextChange[] change = new TextChange[e.Changes.Count]; e.Changes.CopyTo(change, 0); int offset = change[0].Offset; if (change[0].AddedLength > 0) { switch (this.CustomTextBoxType) { case CustomTextBoxType.None: break; case CustomTextBoxType.Integer: NonIntergerHandle(change, offset); break; case CustomTextBoxType.Decimal: NonDecimalHandle(change, offset); break; case CustomTextBoxType.Alphabet: break; case CustomTextBoxType.Alphanumeric: break; case CustomTextBoxType.Currency: break; default: break; } } } private void NonDecimalHandle(TextChange[] change, int offset) { Decimal num = 0; if (!Decimal.TryParse(this.Text, out num)) { this.Text = this.Text.Remove(offset, change[0].AddedLength); this.Select(offset, 0); } } private void NonIntergerHandle(TextChange[] change, int offset) { long num = 0; if (!long.TryParse(this.Text, out num)) { this.Text = this.Text.Remove(offset, change[0].AddedLength); this.Select(offset, 0); } } protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e) { base.OnPreviewKeyDown(e); switch (this.CustomTextBoxType) { case CustomTextBoxType.None: break; case CustomTextBoxType.Integer: IntegerOperation(e); break; case CustomTextBoxType.Decimal: DecimalOperation(e); break; case CustomTextBoxType.Alphabet: break; case CustomTextBoxType.Alphanumeric: break; case CustomTextBoxType.Currency: break; default: break; } } private void DecimalOperation(System.Windows.Input.KeyEventArgs e) { if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || e.Key == Key.Decimal) { if (this.Text.Contains(".") && e.Key == Key.Decimal) { e.Handled = true; return; } e.Handled = false; } else if (((e.Key >= Key.D0 && e.Key <= Key.D9) || e.Key == Key.OemPeriod) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift) { if (this.Text.Contains(".") && e.Key == Key.OemPeriod) { e.Handled = true; return; } e.Handled = false; } else { e.Handled = true; } } private void IntegerOperation(System.Windows.Input.KeyEventArgs e) { if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)) { e.Handled = false; } else if ((e.Key >= Key.D0 && e.Key <= Key.D9) && e.KeyboardDevice.Modifiers != ModifierKeys.Shift) { e.Handled = false; } else { e.Handled = true; } } } public enum CustomTextBoxType { None, Integer, Decimal, Alphabet, Alphanumeric, Currency } }