Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation error template - binding exception

I am using the following template for validation error:

<ControlTemplate>
    <Border BorderBrush="Red" BorderThickness="1">
        <Grid>
            <Polygon Points="8,8 8,0 0,0"
                     Stroke="Black"
                     StrokeThickness="1"
                     Fill="Red"
                     HorizontalAlignment="Right"
                     VerticalAlignment="Top"
                     ToolTip="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
            <AdornedElementPlaceholder x:Name="adorner"/>
        </Grid>
    </Border>
</ControlTemplate>

ToolTip is working fine, but after leaving the current record the following exception is thrown:

System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'ValidationError') from '(Validation.Errors)' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=AdornedElement.(0)[0].ErrorContent; DataItem='AdornedElementPlaceholder' (Name='adorner'); target element is 'Polygon' (Name=''); target property is 'ToolTip' (type 'Object') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

I tried with HasError property, but failed. Anyone has any idea?

like image 813
Goran Avatar asked Sep 21 '12 16:09

Goran


1 Answers

When there are no validation errors, the binding is still trying to access the first element in the ReadOnlyObservableCollection returned by Validation.Errors, but because it is empty, an exception is thrown.

The binding system just swallows the exception, but its still annoying and unnecessary.

Instead of binding like this:

Path=AdornedElement.(Validation.Errors)[0].ErrorContent

..you can do it like this, to avoid the exception:

Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent
like image 173
Peter Hansen Avatar answered Oct 26 '22 06:10

Peter Hansen