[英]WPF TextBox event
I have a textbox and I need the user to enter only Cyrillic letters. 我有一个文本框,我需要用户仅输入西里尔字母。 User can't enter numbers and special characters (except space) and latin characters!
用户不能输入数字和特殊字符(空格除外)和拉丁字符! The Value of variable "l" I will set by myself.
我将自行设置变量“ l”的值。
How can I make the KeyDown event to do this? 我该如何使KeyDown事件做到这一点?
In WindowsForms I do it like this: 在WindowsForms中,我这样做是这样的:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char l = e.KeyChar;
if ((l < 'А' || l > 'я') && l != '\b' )
{
e.Handled = true;
}
}
The most simple way I discovered is to leverage the OnPreviewTextInput
event: 我发现的最简单的方法是利用
OnPreviewTextInput
事件:
Markup: 标记:
<TextBox PreviewTextInput="UIElement_OnPreviewTextInput" />
Handler: 处理器:
private void UIElement_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
bool isCyrillic = Regex.IsMatch(e.Text, @"\p{IsCyrillic}");
e.Handled = !isCyrillic;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.