这是一个WPF/MVVM项目。我使用的是MS Enterprise Library Validation Application Block v5.0。
要求是,如果组合框的值为"设施"或"其他",则注释字段必须有值。
也就是说,我创建了一个自定义验证器,并以这种方式执行验证:
ValidationResults results = Validation.Validate<Annotation>(this.Annotation);
还有其他错误的可能性,这些都包含在标准VAB属性中。
这似乎运行良好。因此,现在,如果我有一个错误条件,它可能是任何一个规则,并且我有ValidationResults集合可供查询,以确定哪个属性有错误。但是,当出现这种情况时,我在为特定元素应用样式时遇到了问题。有一次,我对给定的控件使用了属性级验证,但当我需要为一个验证规则比较多个属性时,这就不起作用了。
vab:Validate.BindingForProperty="Text"
上面的属性和这个样式适用于像StringLenghtValidator这样的简单的单个属性验证。这在我的场景中不起作用。
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="White"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
在多属性自定义验证器报告错误后,如何使Comments文本框具有特定的样式?
我使用这种方法的entreprise库,试试
<ControlTemplate x:Key="ErrorMarkTemplate" TargetType="{x:Type Label}" >
<TextBlock Text="*" Margin="2,0,2,0" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" FontSize="16" Foreground="{DynamicResource ErrorBrush}" />
</ControlTemplate>
<ControlTemplate x:Key="GeneralErrorTemplate" >
<Grid ClipToBounds="False" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Template="{DynamicResource ErrorMarkTemplate}" />
<AdornedElementPlaceholder Grid.Column="1" />
</Grid >
</ControlTemplate>
<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate" Value="{DynamicResource GeneralErrorTemplate}"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding (Validation.Errors).CurrentItem.ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
</Trigger>
</Style.Triggers>
</Style>