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