wpf TextBox只能输入数字

xaml

<TextBox InputMethod.IsInputMethodEnabled=“False” PreviewTextInput=“TextBox_PreviewTextInput” Margin=“10”/>

c#

  1. private void TextBox_PreviewTextInput(object sender, System.Windows.Input.TextCompositionEventArgs e)
  2. {
  3. e.Handled = new Regex(“[^0-9]+”).IsMatch(e.Text);
  4. }