WPF自定义TextBox ErrorTemplate

时间:2013-09-04 13:11:47

标签: wpf validation

我有自定义的AutoCompleteTextBox类,它继承自TextBox。一切正常,除了显示验证错误,即控件周围的红色边框。

这是我的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>

我已经使用Snoop实用程序进行了检查,发现没有红色边框。我已经回顾了WPF的Generic.xaml Aero.Normal.xaml,说实话,我不知道是什么在无效文本框周围绘制了红色验证边框。

我知道之前有过类似的问题,但是我恢复了所有答案,而且没有人帮助过。

1 个答案:

答案 0 :(得分:1)

您将在validation.cs source code中找到默认的ErrorTemplate:

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;
    }

    ...
}

如果要删除它,只需将Validation.ErrorTemplate Attached Property设置为Null。

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