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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With