WPF: Problems with Keyboard Focus When Using Validation

If you use WPF’s data binding with validation a red border appears around controls with invalid values, you can also use the Validation.ErrorTemplate attached property to get a better look for the invalid controls.

But – if you try this you’ll discover that there’s a new tab stop on you from, the red frame (or your own template) now accepts focus, breaking tab navigation.

The problem is that that the validation framework will add a control to the adorner layer in order to display the error indicator – and this control is focusable.

To fix it just add the following to the Window’s resources (or the application resources).

<Style TargetType="{x:Type Control}">
    <Setter Property="Focusable" Value="False"/>
</Style>

This shouldn’t cause any problems because almost no one uses Control itself and this style will not affect subclasses, but you still have to test everything after the change.

posted @ Sunday, May 25, 2008 3:44 PM

Comments on this entry:

# re: WPF: Problems with Keyboard Focus When Using Validation

Left by udallmann at 7/23/2008 10:43 PM

Thanks! This solution works perfectly.
You saved me hours of work!!

# re: WPF: Problems with Keyboard Focus When Using Validation

Left by Dragoljub at 2/5/2009 8:31 PM

Thanks from me too. I was so confused when I finally noticed what was causing the tab to "pause" on my listview (containing textboxes with binding and validation). It was because of the validation. It was not very noticeable because the focus indicator is displayed OVER the red border. Your page was the first result on google! Thanks again!

# re: WPF: Problems with Keyboard Focus When Using Validation

Left by Sekhar at 10/7/2009 5:29 PM

Great ! Worked for me.

Your comment:



 (will not be displayed)


 
 
 
Please add 8 and 2 and type the answer here: