3

This is a WPF/MVVM project. I am using the MS Enterprise Library Validation Application Block v5.0.

The requirement is that if the value of a combobox is "Facilities" or "Other" then the Comment field must have a value.

That said, I created a custom validator and I am performing validation in this manner:

ValidationResults results = Validation.Validate<Annotation>(this.Annotation);

There are other error possibilities and those are covered with the standard VAB attributes.

This seems to be working fine. So, now, if I have an error condition, it could be any one of the rules and I have the ValidationResults collection available to interrogate in order to determine which property has the error. However, I am having trouble with applying a style for a specific element when this occurs. At one point, I was using property level validation for a given control but that doesn't work when I need to compare multiple properties for one validation rule.

vab:Validate.BindingForProperty="Text"

The property above and this style work for a simple single property validation like a StringLenghtValidator.This doesn't work in my scenario.

<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>

How can I get the Comments textbox to have a certain style after the multiple property custom validator reports an error?

chad
  • 564
  • 1
  • 4
  • 16

1 Answers1

0

I'm using entrerprise library with this approach, try it

<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>
Xilmiki
  • 1,453
  • 15
  • 22