And also that the user could not erase the letters Thank you in advance!

  • 3
    Do not torment users with bans. You do not want to curse you? Just convert the entered string to upper case. You can immediately in the course of input, you can then completely. - Sergey

1 answer 1

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
  • one
    You can also remove Ctrl + V if you add DataObject.Pasting="OnPasting" ContextMenu="{x:Null}" in XAML and void 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