JCardenas
JCardenas

Reputation: 41

Validation ErrorTemplate not applied when the control is made visible

I have a control with validation and an error template set up which is not initially visible. The control becomes visible when the property a ContentControl is bound to switches to it. When the control is made visible, however, the error template is only applied after the bound property is updated. Any thoughts why this might be happening and what I can do about it?

XAML control snippet:

<TextBox Name="UserNameTextBox" Grid.Row="0" Grid.Column="1" Style="{StaticResource WinformsErrorTemplate}" Text="{Binding Path=UserName, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" IsEnabled="{Binding Path=CanEditCredentials}"/>

XAML error template:

        <!-- This error template style emulates a Winforms validation error icon -->
        <Style x:Key="WinformsErrorTemplate" TargetType="Control">
            <Setter Property="Margin" Value="3"/>
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel LastChildFill="True">
                            <Ellipse DockPanel.Dock="Right" 
                                     ToolTip="{Binding ElementName=myTextbox, 
                                              Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                                     Width="15" Height="15" 
                                     Margin="-25,0,0,10"
                                     StrokeThickness="1" Fill="Red" >
                                <Ellipse.Stroke>
                                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
                                        <GradientStop Color="#FFFA0404" Offset="0"/>
                                        <GradientStop Color="#FFC9C7C7" Offset="1"/>
                                    </LinearGradientBrush>
                                </Ellipse.Stroke>
                                <Ellipse.Triggers>
                                    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                                        <BeginStoryboard Storyboard="{StaticResource FlashErrorIcon}"/>
                                    </EventTrigger>
                                </Ellipse.Triggers>
                            </Ellipse>
                            <TextBlock DockPanel.Dock="Right" 
                                       ToolTip="{Binding ElementName=myControl, 
                                                Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                                       Foreground="White"
                                       FontSize="11pt" 
                                       Margin="-15,0,0,5" FontWeight="Bold">!
                                <TextBlock.Triggers>
                                    <EventTrigger RoutedEvent="FrameworkElement.Loaded">
                                        <BeginStoryboard Storyboard="{StaticResource FlashErrorIcon}"/>
                                    </EventTrigger>
                                </TextBlock.Triggers>
                            </TextBlock>
                            <Border BorderBrush="Red" BorderThickness="1" Margin="0,0,0,10">
                                <AdornedElementPlaceholder Name="myControl"/>
                            </Border>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <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>

Upvotes: 1

Views: 1531

Answers (2)

Craig Wooldridge
Craig Wooldridge

Reputation: 11

I had the same issue with a program that had multiple views inside a tab control. The validation error would be displayed, but would disappear when I switched to a different tab and back again. Adding the AdornerDecorator to each view fixed the problem. Thanks

Upvotes: 0

JCardenas
JCardenas

Reputation: 41

Found the solution. The issue was that there was an AdornerDecorator at the Window level, but not in any of the UserControls. Consequently, the ErrorTemplate was not able to render. The fix was to encapsulate everything below the UserControl in an AdornerDecorator:

<UserControl>
    <AdornerDecorator>
        <StackPanel>
            ...
        </StackPanel>
    <AdornerDecorator>
</UserControl>

See Validation ErrorTemplate not showing on data errors for more information.

Upvotes: 3

Related Questions