WPF validation red border doesn't show If UserControl collapsed first

Posted by Creepy Gnome on Stack Overflow See other posts from Stack Overflow or by Creepy Gnome
Published on 2010-05-23T01:38:27Z Indexed on 2010/05/23 1:40 UTC
Read the original article Hit count: 317

Filed under:
|
|
|

There seems to be a bug with WPF in 3.5, and I was hoping someone may have found a workaround.

Basically if you have a custom UserControl that contains a TextBox and it is in a Window but initialized to be Collapsed by default in the xaml or code behind if it fails validation when you make the control visible it will not show the red border until it fails while visible.

However, this works correctly when visibility is set to Hidden, just no when Collapsed.

I am already overriding the ErrorTemplate with a style to workaround the Adornment issue with the red border staying visibile when you collapse the control. Below is my full style for the TextBox. If there is any additional changes or additions to make it work correctly with collapsed controls that would be great.

<Style TargetType="TextBox">
    <Setter 
        Property="Margin" 
        Value="3" />
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <ControlTemplate.Resources>
                    <BooleanToVisibilityConverter x:Key="converter" />
                </ControlTemplate.Resources>
                <DockPanel LastChildFill="True">
                    <Border 
                        BorderThickness="2"
                        BorderBrush="Red"
                        Visibility="{
                            Binding ElementName=placeholder, 
                            Mode=OneWay, 
                            Path=AdornedElement.IsVisible, 
                            Converter={StaticResource converter}}"
                        >
                        <AdornedElementPlaceholder x:Name="placeholder" />
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger 
            Property="Validation.HasError"
            Value="true"
            >
            <Setter 
                Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"
                />
        </Trigger>
    </Style.Triggers>
</Style>

© Stack Overflow or respective owner

Related posts about wpf

Related posts about validation