Kevin Burton
Kevin Burton

Reputation: 2282

WPF Error Validation doesn't show ErrorTemplate

I have a window like

<Window.Resources>
    <ControlTemplate x:Key="ValidationTemplate">
        <DockPanel>
            <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
            <AdornedElementPlaceholder/>
        </DockPanel>
    </ControlTemplate>

    <Style x:Key="TextBoxInError" 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>
    </Style>
</Window.Resources>

        <TextBox x:Name="SupportFolder"
                 Validation.ErrorTemplate="{StaticResource ValidationTemplate}"
                 Style="{StaticResource TextBoxInError}"
                 Padding="2">
            <TextBox.Text>
                <Binding Path="Preferences.SupportFolder" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
                    <Binding.ValidationRules>
                        <local:FolderExistsValidationRule ValidationStep="RawProposedValue"/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

So my impression is that when the input for the TextBox is in error that there will be a small red exclamation mark to the left of the TextBox and because of the Style triggers there will be a ToolTip displaying the error. The ToolTip works but I don't see the exclamation point indicating the error. I took this code from the Microsoft Samples but I cannot for the life of me see where I have made a mistake. The sample works (Data Binding -> BindingValidation) but I cannot seem to reproduce the code. Would someone act as a second pair of eyes and see what I am doing wrong?

Upvotes: 0

Views: 49

Answers (1)

mm8
mm8

Reputation: 169160

You should dock the TextBlock in the error template to the left or right:

<TextBlock Foreground="Red" FontSize="20" DockPanel.Dock="Right">!</TextBlock>

Using your current markup, the TextBlock will end up "under" the adorned element.

Upvotes: 1

Related Questions