我可以确保用户只能在 WPF 中的 KeyDown 事件的文本框中输入字母数字和空格吗?不允许使用特殊字符。
最佳答案
检查文本框的keydown事件并执行
// If escape is presses, then close the window.
if( Key.Escape == e.Key )
{
this.Close();
}
// Allow alphanumeric and space.
if( e.Key >= Key.D0 && e.Key <= Key.D9 ||
e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9 ||
e.Key >= Key.A && e.Key <= Key.Z ||
e.Key == Key.Space )
{
e.Handled = false;
}
else
{
e.Handled = true;
}
// If tab is presses, then the focus must go to the
// next control.
if( e.Key == Key.Tab )
{
e.Handled = false;
}
希望这会有所帮助......
关于wpf - 文本框仅包含字母、数字和空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/891518/