第一步 禁掉输入法:

<Window x:Class="WpfModelViewApplication1.Views.MainView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"Title="Main Window" Height="400" Width="800"><Grid x:Name="grid1"><TextBox x:Name="tb" Width="100" HorizontalAlignment="Right" Margin="0,164,122,128"  input:InputMethod.IsInputMethodEnabled="False"/></Grid>
</Window>

第二步 采用正则表达式:

<Window x:Class="WpfModelViewApplication1.Views.MainView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:input="clr-namespace:System.Windows.Input;assembly=PresentationCore"Title="Main Window" Height="400" Width="800"><Grid x:Name="grid1"><TextBox x: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);
}

WPF 怎么让Textbox只能输入数字?相关推荐

  1. wpf怎么让Textbox只能输入数字?

    只需要两步: 1.禁掉输入法: <Window x:Class="WpfModelViewApplication1.Views.MainView"     xmlns=&qu ...

  2. WPF TextBox只能输入数字

    关于WPF TextBox只能输入数字并不是一个很难的功能,网上许多的实现方式都是通过将String类型转换为Int32类型来进行判断的,自从对了Clr Via C#这本书以后就对拆装箱操作产生了恐惧 ...

  3. 限制文本框(TextBox)只能输入数字(VB6代码)

    在一些要求只允许输入数值的场合,就用得上了. 基本思路是,先在文本框的keyPress事件里过滤键盘输入,再使用子类化将粘贴剪切等剪贴板操作吃掉. 窗体frmMain.frm内代码(窗体包含一个文本框 ...

  4. TextBox只能输入数字

    Asp.net TextBox只能输入数字 <asp:textbox id="TextBox1" οnkeyup="if(isNaN(value))execComm ...

  5. 【WinForm】设置控件里面的所有TextBox只能输入数字

    设置控件里面的所有TextBox只能输入数字 回顾 [WinForm]TextBox只能输入数字 只能输入整数 private static void tb_KeyPress(object sende ...

  6. WinForm TextBox 只能输入数字

    在Winform(C#)中要实现限制Textbox只能输入数字,一般的做法就是在按键事件中处理,判断keychar的值.限制只能输入数字,小数点,Backspace,del这几个键.数字0~9所对应的 ...

  7. 【WinForm】TextBox只能输入数字

    只能输入整数 方法一 private void tb_KeyPress(object sender, KeyPressEventArgs e) {//如果输入的不是退格和数字,则屏蔽输入if (!(e ...

  8. Asp.net控制TextBox只能输入数字

    方法一: private void tBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 0x20) e.KeyCh ...

  9. Asp.net TextBox只能输入数字

    <asp:textbox id="TextBox1" οnkeyup="if(isNaN(value))execCommand('undo')" runa ...

最新文章

  1. Ubuntu 14.04 64bit上安装有道词典Linux版本
  2. WebAssembly 系列(五)为什么 WebAssembly 更快?
  3. Leetcode 118. 杨辉三角 (每日一题 20210901)
  4. 让图片算好高宽再显示
  5. c 和 java 差异_java和C/C++的差异是什么
  6. cnn stride and padding_彻底搞懂CNN中的卷积和反卷积
  7. Android应用开发中三种常见的图片压缩方法
  8. 计算机网络知识点总结谢,《计算机网络基础》复习提纲_谢(5)版-课堂教案
  9. jquery删除空图片错误图片,$('img[src=]').remove();
  10. 1.nagios服务端安装
  11. php.ini – 配置文件详解
  12. 安装Python第三方库的基本方法
  13. fiddler抓包教程
  14. SPSS篇—方差分析
  15. Unity3d中XML的解析
  16. CTSC 2018 简要题解
  17. 国内外网站空间优劣势
  18. dxwebsetup 解决一些dll缺少的问题
  19. 2019, XII Samara Regional Intercollegiate Programming Contest 解题报告
  20. 计算机专业刊头,浅谈报纸版面在电脑排版中的美学应用

热门文章

  1. 2017年过去了 我很怀念他
  2. django整体文件结构、配置文件详解
  3. 怀念啊我的青春(歌词)
  4. 新版凯立德地图“减肥”定制方法
  5. 运维学习 之 Ngnix配置(详细版)
  6. 验证码识别(Tess4J初体验)
  7. 100个python算法超详细讲解:卡布列克常数
  8. java 读取文件路径空格、“+”和中文的处理
  9. php设计程序闰年的计算方法,php判断闰年的三种方法(闰年计算方法)
  10. 中国IT公司欣起一场美女程序员鼓励师招聘潮