WPF TextBox限制只能输入数字

WPF TextBox限制只能输入数字

  private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox txt = sender as TextBox;

            // 过滤按键
            if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))
            {
                e.Handled = false;
            }
            else if (((e.Key >= Key.D0 && e.Key <= Key.D9)))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
posted @ 2021-01-18 10:23  循序渐进。  阅读(294)  评论(0)    收藏  举报