当前位置: 代码迷 >> 综合 >> WPF设置文本框只能输入数字
  详细解决方案

WPF设置文本框只能输入数字

热度:11   发布时间:2024-03-07 09:02:11.0
//前台UI设置
<TextBox Width="100" Height="30" PreviewTextInput="TextBox_PreviewTextInput"  PreviewKeyDown="TextBox_PreviewKeyDown" InputMethod.IsInputMethodEnabled="False"/>
InputMethod.IsInputMethodEnabled="False"禁用输入法
后台代码限定只能输入数字
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e){short val;if (!Int16.TryParse(e.Text, out val))e.Handled = true;}private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e){if (e.Key == Key.Space)e.Handled = true;}

 

  相关解决方案