And also that the user could not erase the letters Thank you in advance!
1 answer
private void TextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e) { char inp = e.Text[0]; if (inp < 'А' || inp > 'Я') e.Handled = true; } private void TextBox_OnPreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Back || e.Key == Key.Space) e.Handled = true; } Note that in this version, a hack is possible with Ctrl + V
- And why
base.OnPreviewTextInput(e);? You do not overlap the virtual method? - VladD - @VladD +, I am already writing them on the machine) This call is really not needed. - Nikita
- oneYou can also remove
Ctrl + Vif you addDataObject.Pasting="OnPasting" ContextMenu="{x:Null}"in XAML andvoid OnPasting(object sender, DataObjectPastingEventArgs e) => e.CancelCommand();in code-behind. - VladD - Why do you only analyze
e.Text[0]? And if there are more characters? - VladD - @VladD no more. With each press, exactly one character comes there, and BackSpace and Space are not counted there, so another handler was used for them. - Nikita
|