验证错误模板-绑定异常 [英] Validation error template - binding exception

查看:108
本文介绍了验证错误模板-绑定异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下模板进行验证错误

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可以正常工作,但是在保留当前记录之后,将引发以下异常:

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


System.Windows.Data错误:17:无法从'(Validation.Validation。)获取'Item []'值(类型
'ValidationError')。错误)(类型
'ReadOnlyObservableCollection`1')。
BindingExpression:Path = AdornedElement。(0)[0] .ErrorContent;
DataItem = AdornedElementPlaceholder(名称=装饰者);目标元素
是多边形(名称=);目标属性为工具提示(类型为对象)
ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException:
指定的参数不在有效值范围内。

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.

我尝试使用 HasError 属性,但失败了。任何人有任何想法吗?

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

推荐答案

如果没有验证错误,则绑定仍尝试访问ReadOnlyObservableCollection中的第一个元素由Validation.Errors返回,但是因为它为空,所以抛出了异常。

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

这篇关于验证错误模板-绑定异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆