[英]WPF TextBox event
我有一個文本框,我需要用戶僅輸入西里爾字母。 用戶不能輸入數字和特殊字符(空格除外)和拉丁字符! 我將自行設置變量“ l”的值。
我該如何使KeyDown事件做到這一點?
在WindowsForms中,我這樣做是這樣的:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char l = e.KeyChar;
if ((l < 'А' || l > 'я') && l != '\b' )
{
e.Handled = true;
}
}
我發現的最簡單的方法是利用OnPreviewTextInput
事件:
標記:
<TextBox PreviewTextInput="UIElement_OnPreviewTextInput" />
處理器:
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.