900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > WPF 文本框只能输入数字

WPF 文本框只能输入数字

时间:2022-02-09 15:06:39

相关推荐

WPF 文本框只能输入数字

只需要两步:

1.禁掉输入法:

<Windowx:Class="WpfModelViewApplication1.Views.MainView"

xmlns="/winfx//xaml/presentation"

xmlns:x="/winfx//xaml"

xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"

Title="MainWindow"Height="400"Width="800">

<Gridx:Name="grid1">

<TextBoxx:Name="tb"Width="100"HorizontalAlignment="Right"Margin="0,164,122,128"input:InputMethod.IsInputMethodEnabled="False"/>

</Grid>

</Window>

第二步 采用正则表达式:

Title="MainWindow"Height="400"Width="800">

<Gridx:Name="grid1">

<TextBoxx:Name="tb"Width="100"HorizontalAlignment="Right"Margin="0,164,122,128"input:InputMethod.IsInputMethodEnabled="False"/>

</Grid>

</Window>

第二步 采用正则表达式:

<Windowx:Class="WpfModelViewApplication1.Views.MainView"

xmlns="/winfx//xaml/presentation"

xmlns:x="/winfx//xaml"

xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"

Title="MainWindow"Height="400"Width="800">

<Gridx:Name="grid1">

<TextBoxx:Name="tb"Width="100"HorizontalAlignment="Right"Margin="0,164,122,128"PreviewTextInput="tb_PreviewTextInput"input:InputMethod.IsInputMethodEnabled="False"/>

</Grid>

</Window>

cs后台代码:

//using System.Text.RegularExpressions;private void tb_PreviewTextInput(object sender, TextCompositionEventArgs e){Regex re = new Regex("[^0-9.-]+");e.Handled = re.IsMatch(e.Text);}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。