【发布时间】:2012-04-02 21:38:01
【问题描述】:
我正在使用控件模板来显示每个控件上的验证错误,使用内置 WPF 的验证机制,一切正常。 controlTemplate 如下所示:
<ControlTemplate x:Key="MyErrorTemplate" TargetType="{x:Type Control}">
<StackPanel Orientation="Horizontal">
<Border BorderBrush="Red" BorderThickness="2" CornerRadius="3">
<AdornedElementPlaceholder Name="MyAdorner" />
</Border>
<Image Name="imgError"
Source="/MyAssembly;component/Images/ValidationIcon.png"
ToolTip="{Binding ElementName=MyAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
</StackPanel>
</ControlTemplate>
我已经读到,只要控件出错,验证机制就会将经过验证的控件与控件模板(默认模板或上述自定义模板)一起包装起来。
"当 WPF 验证系统检测到它创建的无效控件时 和装饰器,它持有一个控件(...),将一个控件插入其中并设置该控件 模板到附加的 Validation.ErrorTemplate 的内容 属性。
它将装饰器定位在原始控件之上,以便 AdornedElementPlaceholder 正好在控件之上,这让我们 轻松将控件模板内容相对于原始内容放置 控制”(see more)
如何为其他功能执行相同的行为?我的意思是在没有 WPF 验证系统的情况下使用“MyErrorTemplate”,可以吗?
【问题讨论】:
-
您在寻找 IDataErrorInfo 接口吗? WPF 使用此接口来验证对象。 (见这里japikse.blogspot.fr/2009/07/…)
-
不,我不是。我想在不使用验证机制的情况下将相同的验证模板分配给控件
标签: wpf validation