0

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

这是我的 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,老实说,我不知道是什么在无效文本框周围绘制了红色验证边框。

我知道以前曾提出过类似的问题,但我重新审查了所有答案,但没有一个有帮助。

4

1 回答 1

1

您将在validation.cs 源代码中找到默认的 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}" />
于 2013-09-04T13:32:13.557 回答