/// <summary>
    /// 允许输入数字的TextBox,禁止粘贴,允许backspace、left、right、enter键
    /// </summary>
    public class NumberTextBox : TextBox
    {
        public NumberTextBox()
        {
            DataObject.AddPastingHandler(this, Text_Pasting);
        }

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9 && e.KeyboardDevice.Modifiers != ModifierKeys.Shift)
                || (e.Key >= Key.D0 && e.Key <= Key.D9 && e.KeyboardDevice.Modifiers != ModifierKeys.Shift) ||
                e.Key == Key.Back || e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Enter)
            {
                if (e.KeyboardDevice.Modifiers != ModifierKeys.None)
                {
                    e.Handled = false;
                }
            }
            else
            {
                e.Handled = true;
            }
        }

        private void Text_Pasting(object sender, DataObjectPastingEventArgs e)
        {
            //禁止Pasting
            e.CancelCommand();
        }
    }

  上述代码屏蔽了小数点输入。

相关文章:

  • 2021-11-27
  • 2021-11-18
  • 2021-11-18
  • 2021-09-13
  • 2020-06-23
  • 2017-12-14
  • 2021-11-18
猜你喜欢
  • 2021-11-04
  • 2021-12-04
  • 2021-11-18
  • 2021-11-18
  • 2021-08-01
  • 2021-11-27
  • 2019-07-24
  • 2021-12-03
相关资源
相似解决方案