wpf - 文本框仅包含字母、数字和空格

标签 wpf

我可以确保用户只能在 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/

相关文章:

c# - Windows 10 通用应用程序文件/目录访问

c# - WPF MVVM 根据焦点项目执行不同的操作

c# - 如何在ItemsControl中显示弹出窗口

c# - 如何将 DateTime 的 "time"转换为 int?

c# - 如何设置字符串属性 C# EF 的分隔词的最大长度

c# - 使用 C# 将 WPF 文件转换为 PNG 文件

c# - 如何将此语句转换为XAML?

c# 程序集与 wpf 应用程序绑定(bind)

c# - 使用 XamlReader 创建的 DataTemplate 无法找到 StaticResources

wpf - 证书和问题的正确答案