创建仅接受Text的WPF TextBox

时间:2013-02-15 05:15:32

标签: wpf

private void txtLastName_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (!char.IsDigit((char)e.Key)) e.Handled = true;
    }

但它不支持键盘上的所有键。

2 个答案:

答案 0 :(得分:2)

您必须使用IsLetter

private void txtLastName_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (Char.IsLetter((char)e.Key)) e.Handled = true;
    }

答案 1 :(得分:2)

    private void txtLastName_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        if (!System.Text.RegularExpressions.Regex.IsMatch(e.Text, "^[a-zA-Z]"))
        {
            e.Handled = true;
        }
    }