views:

655

answers:

3

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

thanks

A: 

Found a workaround that works put a AdornerDecorator around the textbox

Rick
what's the workaround?
Shafique
A: 

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

Check this answer or this answer for potential solutions. Either of these two will work for you.

Drew Noakes