Tony Vitabile
Tony Vitabile

Reputation: 8594

TextBox validation is not displaying errors

I've built a form for editing data in my WPF application. I'm in the process of adding validation to the form. I started by using this article and this one, but the error template either shows all of the time or not at all. I don't know what I'm doing wrong.

Here are the ControlTemplate and Style that I'm using. They are in the form's Resources:

<ControlTemplate x:Key="TextBoxErrorTemplate">
    <StackPanel ClipToBounds="False" Orientation="Horizontal">
        <Border BorderBrush="Red"
                BorderThickness="1"
                Margin="15,0,0,0">
            <AdornedElementPlaceholder Name="adornedElement" />
        </Border>
        <Image HorizontalAlignment="Right"
               VerticalAlignment="Top"
               Width="20"
               Height="20"
               Margin="0,-5,-5,0"
               Source="{StaticResource ErrorImage}"
               ToolTip="{Binding Converter={StaticResource ErrorConverter}, 
                                 ElementName=adornedElement, 
                                 Path=AdornedElement.(Validation.Errors)}" />
    </StackPanel>
</ControlTemplate>

<Style x:Key="TextBoxErrorStyle" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"
                    Value="Binding Converter={StaticResource ErrorConverter}, 
                                   RelativeSource={x:Static RelativeSource.Self}, 
                                   Path=AdornedElement.(Validation.Errors)}"/>
        </Trigger>
    </Style.Triggers>
</Style>

And here's the TextBox that uses these parts:

<TextBox Grid.Column="0"
            Margin="5,0"
            MaxLength="50"
            Name="NameBox"
            TabIndex="0"
            Validation.ErrorTemplate="{StaticResource TextBoxErrorTemplate}"
            Style="{StaticResource TextBoxErrorStyle}"
            TextAlignment="Left"
            TextChanged="NameBox_TextChanged"
            VerticalAlignment="Center"
            Visibility="{Binding Converter={StaticResource InvertedBoolToVisibility}, Path=AutoConfigureCameras, RelativeSource={RelativeSource AncestorType={x:Type cs:EditLPRDetails}}}">
    <TextBox.Text>
        <Binding Mode="TwoWay" Path="Name" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <cs:RegexValidationRule Pattern="{StaticResource NamePattern}" />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

Note that the validation logic in the RegexValidationRule class works. When I put a valid string into the TextBox, it returns success, and when I put an invalid string into it, it returns failure. Whatever is wrong, I believe the problem is in the Style's Trigger.

Upvotes: 1

Views: 388

Answers (2)

Tony Vitabile
Tony Vitabile

Reputation: 8594

I found the answer to my problem.

It turns out that my dialog box contains a TabControl and that is the cause of the issue. I found the answer in this article. Essentially, I need to put the contents of the TabItem that contains the controls being validated inside of an AdornerDecorator control, which is itself inside of a Border control. Once that is done, then the error indicators all display properly.

I did not include the fact that my controls were inside of a TabControl at first because I didn't know that it mattered. Live and learn.

Upvotes: 0

Nitin Purohit
Nitin Purohit

Reputation: 18580

You are close, the Setter Value syntax for binding is not correct, plus you should set Path as Validation.Errors

 <Setter Property="ToolTip"
         Value="{Binding Converter={StaticResource ErrorConverter},
                         RelativeSource={x:Static RelativeSource.Self},
                         Path=(Validation.Errors)}"/>

Upvotes: 2

Related Questions