Validation Template without Binding Errors

WPF guru Josh Smith has just put up a great post here about how to access the validation errors on a WPF control without getting lots of binding debug output. Most samples (including MSDN documentation) suggest you should use Validation.Errors[0] which generate debug output for a binding failure whenever there are no errors (since Errors[0] doesn’t exist).

The format of the template that I commonly use (which also avoids the Errors[0] issue in almost exactly the same way) is as follows:

        <ControlTemplate x:Key="DefaultErrorTemplate">
            <DockPanel DataContext="{Binding ElementName=adorner, 
ErrorContent}"> <Ellipse x:Name="Ellipse"
="14" Height="14"
="Center" Stroke="#40000000" StrokeThickness
="2" Fill="Red"> <Ellipse.ToolTip> <Border MaxWidth="350"> <ContentControl FontSize="14" Content="{Binding}"/> </Border> </Ellipse.ToolTip> </Ellipse> <Border BorderBrush="#40FFAF00" BorderThickness="2" IsHitTestVisible="False"> <Border.Background> <SolidColorBrush Color="Red" Opacity="0.2"/> </Border.Background> <AdornedElementPlaceholder Margin="-2" Name="adorner"/> </Border> </DockPanel> </ControlTemplate>

Normally instead of hard coding the colours I would use a colour converter that maps the severity of the error (through a custom property) to the relevant colour, e.g. error=red, warning=orange etc.

My equivalent of Josh’s sample project is here.


One thought on “Validation Template without Binding Errors”

Comments are closed.