2

I've got a style for a TextBox to show a validation error message as follows:

<Style TargetType="{x:Type TextBox}">
       <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>
       <Setter Property="Validation.ErrorTemplate">
           <Setter.Value>
               <ControlTemplate>
                   <StackPanel Orientation="Horizontal">
                       <Border BorderBrush="{Binding Path=ErrorContent, 
                               Converter={StaticResource ValidationErrorToBrushConverter}}" BorderThickness="2">
                           <AdornedElementPlaceholder />
                       </Border>
                       <Image Name="image1" Height="14" Width="14" Stretch="Fill" Margin="1,1,1,1" 
                              Source="{Binding Path=ErrorContent, 
                              Converter={StaticResource ValidationErrorToImageSourceConverter}}" 
                              ToolTip="{Binding Path=ErrorContent}"/>
                   </StackPanel>
               </ControlTemplate>
           </Setter.Value>
       </Setter>
   </Style>

The TextBox lives in an Expander. When I open the Expander, the TextBox allows for input, but will fail validation if the input is NullorEmpty, or contains special characters.

My problem is that when I trigger a validation error, the TextBox lights up in red and shows an icon with the message as a tooltip. All good so far. BUT when i close the Expander without passing validation, the red outline and icon with tooltip are still there! Even with the Expander shrunk down! Just floating there... This is not good behavior.

Any ideas on how to get the Validation stuff to hide along with all the other controls in the Expander? Also, the Style for validation is declared in the resources of the UserControl, not in the Expander itself.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Shafique
  • 1,828
  • 6
  • 26
  • 36
  • Check [this answer](http://stackoverflow.com/questions/1471451/wpf-error-template-red-box-still-visible-on-collapse-of-an-expander/1471733#1471733) or [this answer](http://stackoverflow.com/questions/321327/how-do-i-get-rid-of-the-red-rectangle-when-my-wpf-binding-validation-has-failed-a/321987#321987) for potential solutions. Either of these two will work for you. – Drew Noakes Oct 19 '09 at 10:23

3 Answers3

0

I ended up simply clearing the TextBox upon closing the Expander. That way, the validation error goes away and the box is clear and ready for another input when the Expander is opened back up.

Shafique
  • 1,828
  • 6
  • 26
  • 36
0

I had the same problem. I fixed it by putting an AdornerDecorator as the first child object of the expander. The AdornerDecorator is collapsed when the Expander is collapsed, so the Adorners should all disappear too.

NielW
  • 3,626
  • 1
  • 30
  • 38
0

I've resolved this same problem by setting the Validation.ErrorTemplate property to null when the TextBox is hidden

<Style TargetType="TextBox">
    <Style.Triggers>
        <Trigger Property="IsHitTestVisible" Value="False">
            <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        </Trigger>
    </Style.Triggers>
</Style>
Ben Adelson
  • 33
  • 1
  • 4