#632 – 使用PreviewTextInput事件阻止用户输入(Block Input Using PreviewTextInput)
来源:互联网 发布:sublime text3配置php 编辑:程序博客网 时间:2024/05/03 12:30
你可以使用控件的PreviewTextInput 事件来阻止控件接收某些输入的字符。
要防止某个特定的字符输入到控件,你只要简单的设置TextCompositionEventArgs 类型参数的Handled 属性为true就可以。它将中断控件接收字符输入事件的路由。
在XAML代码中注册事件:
<TextBox Text="" HorizontalAlignment="Center" Width="150" PreviewTextInput="TextBox_PreviewTextInput" />
CS文件代码中实现事件处理函数:
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { // No e's allowed if ((e.Text == "e") || (e.Text == "E")) e.Handled = true; }
在代码中,我们组织了‘e’和‘E’这两个字符的输入,在TextBox 中输入这两个字符将不会被接收。
要注意有些按键按下是不会触发PreviewTextInput 事件的,它们是:
-空格(Spacebar)
-回退(Backspace)
-Home/End/Delete/Insert 键
-方向箭头
-Ctl组合键, 比如 Ctrl+V
原文地址:https://wpf.2000things.com/2012/08/24/632-block-input-using-previewtextinput/
0 0
- #632 – 使用PreviewTextInput事件阻止用户输入(Block Input Using PreviewTextInput)
- #630 – PreviewTextInput 和TextInput 事件(PreviewTextInput and TextInput Events)
- WPF TextBox PreviewTextInput handle IME (chinese)
- 阻止input文本框输入回车键时,出现表单提交事件
- ios 阻止用户输入表情
- input 输入事件
- Input Events(输入事件)
- input输入事件
- 使用oninput事件跟踪显示input的输入
- dojo中DateTextBox阻止用户输入
- Android input输入事件处理
- Input Events(输入事件)
- Android input输入事件处理
- JS阻止用户默认事件的发生
- 使用jQuery为input输入框绑定回车键事件 VS 为a标签绑定click事件
- <input type="file">禁止用户输入
- python用户输入问题,input()和raw_input()
- Android 2.3 input输入事件处理
- LayoutManagerDemo:仿探探、人人影视 卡片层叠 炫动滑
- oracle 利用存储过程为表动态建立索引
- http: server gave HTTP response to HTTPS client
- JSP 页面中对Cookie的操作
- Unity中调用文件选择对话框
- #632 – 使用PreviewTextInput事件阻止用户输入(Block Input Using PreviewTextInput)
- 【深度学习:CNN】BP算法
- 自动化测试需要的环境变量
- 3427: Poi2013 Bytecomputer
- Namenode HA
- 决策树
- runOnUiThread更新主线程
- 最全ASCII对应码表-键值
- JSP学习笔记七之Cookie