locked
WPF - TextBox Validation and LostFocus RRS feed

  • Question

  • Greetings Folks,

    I'm new to WPF so any help you can give would be greatly appreciated. I have a simple text box which is bound to a Double. I have implemented its validation via a custom 'ErrorTemplate' in the binding; triggered when the user presses a key ( UpdateSourceTrigger="PropertyChanged" ), as mentioned in the documentation, and the validation aspect seems to be working fine. However, I cannot seem to prevent the user shifting focus from my text box to another control when the contents of the text box is invalid.

    I've tried providing a LostFocus event handler in which I can determine if the text box contents is in error but setting the 'Handled' property of the RoutedEventArgs argument does not prevent the focus being shifted to another control (it does, however, leave a 'ghost' carat in the text box itself - which is wierd). I even tried setting the focus back to my text box with the LostFocus event handler but all I got was a stack overflow exception.

    All I really want to be able to do is prevent the user from leaving the text box until a valid value has been entered.

    Can anyone help??

    Cheers,

    RobDev
    Tuesday, April 14, 2009 1:05 PM

Answers

  • Hi Sergi and Jim,

    sorry about not replying sooner but I've been busy with other things.

    Sergi, I've tried out your suggestion of creating a PreviewLostKeyboardFocus handler and it works - brilliant!!

    Jim, my original code for the LostFocus hander was as follows:

     private void textBox2_LostFocus(object sender, RoutedEventArgs e)
        {
    
            bool isInError = Validation.GetHasError(textBox2);
    
            if (isInError)
            {
                e.Handled = true;
                // textBox2.Focus();    // this causes a stack overflow 
                                       // exception.
            }
        }
    Many thanks for your help, gentlemen.

    RobDev
    • Marked as answer by RobDev Thursday, April 16, 2009 12:30 PM
    Thursday, April 16, 2009 12:30 PM

All replies

  • You could try the PreviewLostKeyboardFocus. If you set that event to handled, the msdn documentation states that the focus doesn't change.
    Here:

    http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewlostkeyboardfocus.aspx

    http://msdn.microsoft.com/en-us/library/system.windows.input.keyboardfocuschangedeventhandler.aspx

    I don't know what happens if you have different focus scopes (like, when you go to the menu). Try to test it throughly.
    • Proposed as answer by Sergi Diaz Thursday, April 16, 2009 12:49 PM
    Tuesday, April 14, 2009 1:25 PM
  • Hi,

     

    -->I cannot seem to prevent the user shifting focus from my text box to another control when the contents of the text box is invalid

     

    Try to call the Focus method like this:

    Keyboard.Focus (otherControl):

    To set keyboard focus to other controls.

     

    -->I even tried setting the focus back to my text box with the LostFocus event handler but all I got was a stack overflow exception.

     

    Could you please provide a simplified example to demonstrate the issue?

     

    Thanks.


    Jim Zhou -MSFT
    Wednesday, April 15, 2009 7:00 AM
  • Hi Sergi and Jim,

    sorry about not replying sooner but I've been busy with other things.

    Sergi, I've tried out your suggestion of creating a PreviewLostKeyboardFocus handler and it works - brilliant!!

    Jim, my original code for the LostFocus hander was as follows:

     private void textBox2_LostFocus(object sender, RoutedEventArgs e)
        {
    
            bool isInError = Validation.GetHasError(textBox2);
    
            if (isInError)
            {
                e.Handled = true;
                // textBox2.Focus();    // this causes a stack overflow 
                                       // exception.
            }
        }
    Many thanks for your help, gentlemen.

    RobDev
    • Marked as answer by RobDev Thursday, April 16, 2009 12:30 PM
    Thursday, April 16, 2009 12:30 PM