views:

537

answers:

1

Hi. I'm using a WPF validation for TextBox validation. I have defined this Template:

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}" BasedOn="{StaticResource StyleTextBox}">        
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
                <Setter Property="Background" Value="{StaticResource TextBox_ErrorBackgroundBrush}"/>
                <Setter Property="BorderBrush" Value="{StaticResource TextBox_ErrorBorderBrush}"/>
                <Setter Property="BorderThickness" Value="2"/>                      
            </Trigger>
        </Style.Triggers>
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel>
                        <TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="20" Text="!"/>
                        <AdornedElementPlaceholder/>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

The TextBox is located on a form in a TabItem. Everything works fine, but the '!' TextBlock stays visible when I choose other TabItems. This behaviour is observed in many other cases - when expander expands etc.. the Excklamation always stays visible on the same place, although the TextBox is not diplayed.

+3  A: 

This is what we do...

<Style x:Key="ErrorTemplate" TargetType="Control">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <!--Set your error template in here-->
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsVisible" Value="false">
            <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        </Trigger>
    </Style.Triggers>
</Style>
This has one problem ! In SOME cases, the WPF throws and Exception by rendering (cannot add value NULL to the collection). It happens when I have the TextBox inside of an UserControl and I save it in the cache and I reload the instance from the cache.
PaN1C_Showt1Me