首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF自定义TextBox ErrorTemplate

WPF自定义TextBox ErrorTemplate
EN

Stack Overflow用户
提问于 2013-09-04 21:11:47
回答 1查看 830关注 0票数 0

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

这是我在Generic.xaml上的风格。

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

我知道以前有过类似的问题,但我复习了所有的答案,没有一个有帮助。

EN

回答 1

Stack Overflow用户

发布于 2013-09-04 21:32:14

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

代码语言:javascript
复制
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。

代码语言:javascript
复制
<TextBox Validation.ErrorTemplate="{x:Null}" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18614827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档