adontz
adontz

Reputation: 1438

WPF Custom TextBox ErrorTemplate

I have custom AutoCompleteTextBox class which inherits from TextBox. Everything works just fine, except showing validation error, i.e. red border around the control.

Here is it's style from my Generic.xaml.

    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="Padding" Value="2" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:AutoCompleteTextBox}">
                <Grid>
                    <Border x:Name="PART_Border" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Border>
                    <Popup x:Name="PART_Popup" StaysOpen="False">
                        <ListBox x:Name="PART_ListBox" HorizontalContentAlignment="Stretch" />
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Background" TargetName="PART_Border" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                    <Trigger Property="IsReadOnly" Value="true">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I've checked with Snoop utility and found no red border. I've reviewed WPF's Generic.xaml Aero.Normal.xaml and, to be honest, have no idea what draws that red validation border around invalid textbox.

I know similar questions were assed before, but I reviwed all answers and non of them helped.

Upvotes: 0

Views: 867

Answers (1)

LPL
LPL

Reputation: 17063

You will find the default ErrorTemplate in validation.cs source code:

public static class Validation
{ 
    /// <summary>
    ///     Template used to generate validation error feedback on the AdornerLayer.  Default
    ///     Template is:
    /// <code>
    ///     <border borderthickness="1" borderbrush="Red">
    ///        <adornedelementplaceholder>
    ///     </adornedelementplaceholder></border>
    /// </code>
    /// </summary>
    public static readonly DependencyProperty ErrorTemplateProperty =
            DependencyProperty.RegisterAttached("ErrorTemplate",
                        typeof(ControlTemplate), typeof(Validation),
                        new FrameworkPropertyMetadata(
                            CreateDefaultErrorTemplate(),
                            FrameworkPropertyMetadataOptions.NotDataBindable,
                            new PropertyChangedCallback(OnErrorTemplateChanged))); 

    private static ControlTemplate CreateDefaultErrorTemplate()
    {
        ControlTemplate defaultTemplate = new ControlTemplate(typeof(Control));

        //<border borderthickness="1" borderbrush="Red">
        //        <adornedelementplaceholder>
        //</adornedelementplaceholder></border>

        FrameworkElementFactory border = new FrameworkElementFactory(typeof(Border), "Border");
        border.SetValue(Border.BorderBrushProperty, Brushes.Red);
        border.SetValue(Border.BorderThicknessProperty, new Thickness(1));

        FrameworkElementFactory adornedElementPlaceHolder = new FrameworkElementFactory(typeof(AdornedElementPlaceholder), "Placeholder");

        border.AppendChild(adornedElementPlaceHolder);

        defaultTemplate.VisualTree = border;
        defaultTemplate.Seal();

        return defaultTemplate;
    }

    ...
}

If you want to remove it just set Validation.ErrorTemplate Attached Property to Null.

<TextBox Validation.ErrorTemplate="{x:Null}" />

Upvotes: 1

Related Questions