命名空间,using System.Text.RegularExpressions;
在xmal中,<Grid
>
<
TextBox
Name
=
"textBox1"
PreviewTextInput
=
"previewTextInput"
/>
</
Grid
>
加入函数,
private
void
previewTextInput(
object
sender, TextCompositionEventArgs e)
{
Regex re =
new
Regex(
"[^0-9]+"
);
e.Handled = re.IsMatch(e.Text);
}
若要支持小数点,为"[^0-9.]+",还要支持负号,为"[^0-9.-]+"