Keeping Element Focused on Validation Error

Requirement is to focus the control on ValidationRule fail.

Solution with explanation: Handling of focus is done by framework and it explicitly change the focus based on error event. Now, if requirement is to retain/bring the focus then one has to call a method Focus() with input priority to re-focus the element or the control.

Code

Code to achieve this is:
 

private void FocusControlOnError(object sender, ValidationErrorEventArgs e)

{

    if (e.OriginalSource is IInputElement)

    {

        this.Dispatcher.BeginInvoke(DispatcherPriority.Input, (ThreadStart)delegate()

        {

            (e.OriginalSource as IInputElement).Focus();

        });

    }

}

 

 

 

 

 

Ebook Download
View all
Learn
View all