I’m using a PasswordBox control in wpf.
I want to block space in the control, but i can’t find the way. I tried to use a KeyDown event but the event isn’t working.
What is the best way to block space in the PasswordBox?
I’m using a PasswordBox control in wpf.
I want to block space in the control, but i can’t find the way. I tried to use a KeyDown event but the event isn’t working.
What is the best way to block space in the PasswordBox?
On
The KeyDown event you're handling actually fires after the character added to the passwordbox. You can block it by handling PreviewKeyDown this way(KeyDown won't fire anymore if user pressed space):
private void passwordbox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
e.Handled = true;
}
}
If you need to block an event you're going to use the event beginning with "Preview"(read more here).
For WPF you should using
PreviewKeyDownBased on docs occurs before theKeyDownevent When a key is pressed while focus is on this control.XAML:
and in behind :
Also in C# native try this :