文本框仅包含字母,数字和空格

Sau*_*ron 3 wpf

我可以确保用户只能在WPF中的KeyDown事件的文本框中输入字母数字和空格吗?不允许使用特殊字符.

Sau*_*ron 6

检查文本框的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;
            }
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助......