I check the validity of the entered data in the LostFocus event (by the way, no one knows how to do it better? It was impossible to enter incorrect data in general, such as MaskedTextBox from Windows Forms.) And if incorrect data is entered, then I display an error and want to set the focus back. But instead, I have some sort of endless recursion and throws out the stack overflow error.

private void tbBottom_LostFocus(object sender, RoutedEventArgs e) { var value = 0.0; if (!double.TryParse(tbBottom.Text, out value) || value < 0 || value > 10000 || BottomValue >= TopValue) { MessageBox.Show("Incorrectly set. The value must adhere to the following rules: Bottom >= 0 && Bottom < 10000 && Bottom < Top.", "Setting error!", MessageBoxButton.OK, MessageBoxImage.Error); tbBottom.Focus(); return; } BottomValue = value; } 
  • 2
    Insert the code, select it and press ctrl + K (this is the question of how to insert the code here) - Batanichek

2 answers 2

From here :

GotFocus, Leave, LostFocus, Validating, or Validated event handlers.

You cannot set focus from a LostFocus handler.

And here is a little about validation in WPF.

  • so same Windows Forms like. There is no such event in WCF. - PECHAPTER
  • Kozyak, the second tag did not notice. But the rule is that it is impossible to set focus in the MS focus change handler for MS, it seems like it applies to everything, starting with WinAPI. - Alekcvp

Why not use a ready-made control with a mask from the Extended WPF Toolkit ?

 <xctk:MaskedTextBox x:Name="_maskedTextBox" Mask="(000) 000-0000" ValueDataType="{x:Type s:String}" /> 

More here

Download here

  • I'm not sure that this control can ask everything exactly as I need it. Well, let's say what kind of mask I need to ask so that there are only digits + so that the number is> = 0 and <= 10,000. So, besides, I also have a comparison with another variable, and here I think the mask will not help anything. - PECHAPTER
  • @DarkByte to enter only numbers - a mask will do. As for the rest, I propose to abandon the focus of the control and use the TextBox.TextChanged event and check the text of the control for validity every time it is modified. - Yurii Manziuk
  • why suddenly TextChanged? LostFocus is much more convenient, why is it necessary that when you type each character, an event will work? Better just once with a loss of focus. I'm telling me just the problem with setting the focus back in case of an error. - PECHAPTER
  • @DarkByte is quite an adequate other answer) with tricks never worked and most likely I will not because of incorrect triggering from time to time. Fighting it is more expensive - Yurii Manziuk
  • TextChanged is also terribly inconvenient and clumsy thing, I tried to insert the same code into the TextChanged (LostFocus handler) handler right now and then it works when everything is correct, and when it is not right it does not work. Yes, even this messagebox on every incorrectly entered digit infuriates. It would be better to have something more interactive ... - PECHAPTER