如何在 wpf 应用程序中使文本框成为数字



我想将文本框制作为数字,我尝试了这个函数:

   <TextBox Name="txtProductId" PreviewTextInput="Number_Validation"/>

   public static void Number_Validation(object sender,System.Windows.Input.TextCompositionEventArgs e)
   {
       System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("[^0-9]+");
       e.Handled = regex.IsMatch(e.Text);
   }

但它接受"空间"和数字。我不需要空间。

"^[0-9]+" .^ 应该在外面

在正则表达式中,您需要定义在输入的开始(^(和结束($(之间,只有十进制字符有效,在这种情况下,表达式看起来像

^d+$

要检查您的具体要求,请使用正则表达式101在线。

最新更新