验证控件模板字符串始终显示,即使没有验证错误

本文关键字:验证 错误 控件 字符串 显示 | 更新日期: 2023-09-27 17:56:44

>我创建了一个样式,以便指定在某些文本框中存在验证错误时要使用的验证控件模板。验证错误被捕获,因为我可以看到我的样式被使用(文本框的默认红色边框,以及我添加的浅粉色背景和红色"!!"字符串),但问题是"!!"红色字符串始终存在,即使没有验证错误(尽管粉红色背景和红色边框消失了)。我正在使用IDataErrorInfo来验证文本框。

这是我的 xaml 样式代码:

<Style x:Key="ErrorValidationTextBox" TargetType="{x:Type pres:OneTextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="Background" Value="LightPink"></Setter>
        </Trigger>
    </Style.Triggers>
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <AdornedElementPlaceholder x:Name="ControlWithError"/>
                    <TextBlock DockPanel.Dock="Right"
                                Foreground="Red"
                                FontSize="12pt"
                                FontWeight="Bold"
                                Margin="-18,0,0,0"
                                Text="!!!">
                    </TextBlock>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我尝试将此行添加到节点,但它不起作用:

Visibility="{Binding Validation.HasError, Source={RelativeSource Self}, Converter={StaticResource BoolToHiddenOrVisibleConverter}}"

我的问题是:如何使"!!"红色字符串仅在 Validation.HasError 标志设置为 true 时才出现?

以下是我声明我的一个文本框作为参考的方式。OneTextBox 是一个封装常规 WPF 文本框并添加一些功能的控件(因为我使用的是自定义框架):

    <pres:OneTextBox Grid.Row="0" Watermark="Name..." Margin="85,12,0,0" Style="{StaticResource ErrorValidationTextBox}"
                     Text="{Binding Path=InterfaceSpecification.Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
                     AcceptsReturn="False" MaxLines="1" Height="22" VerticalAlignment="Top"
                     HorizontalAlignment="Left" Width="300" />

编辑:在调试中运行时出现以下绑定错误:

BindingExpression path error: 'Validation' property not found on 'object' ''RelativeSource' (HashCode=58276509)'. BindingExpression:Path=Validation.HasError; DataItem='RelativeSource' (HashCode=58276509); target element is 'TextBlock' (Name=''); target property is 'Visibility' (type 'Visibility')

EDIT2:以下是我在类中实现IDataErrorInfo的方法:

    public string Error
    {
        get { return mError; }
        set { mError = value; }
    }
    public string this[string columnName]
    {
        get
        {
            switch (columnName)
            {
                case "Name":
                    if (string.IsNullOrWhiteSpace(Name))
                    {
                        Error = "The name cannot be null, empty or contain only white spaces";
                    }
                    else if (Name.StartsWith(" "))
                    {
                        Error = "The name cannot start with a white spaces";
                    }
                    else if (Name.IndexOfAny(Path.GetInvalidPathChars()) != -1)
                    {
                        Error = "The name cannot contain invalid characters";
                    }
                    else
                    {
                        Error = null;
                    }
                    break;
            }
            return Error;
        }
    }

验证控件模板字符串始终显示,即使没有验证错误

肯定有效

<Style x:Key="ErrorValidationTextBox" TargetType="{x:Type pres:OneTextBox}">
<Style.Triggers>
    <Trigger Property="Validation.HasError" Value="True">
        <Setter Property="Background" Value="LightPink"></Setter>
    </Trigger>
</Style.Triggers>
<Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate>
            <DockPanel LastChildFill="True">
                <AdornedElementPlaceholder x:Name="ControlWithError"/>
                <TextBlock DockPanel.Dock="Right"
                            Foreground="Red"
                            FontSize="12pt"
                            FontWeight="Bold"
                            Margin="-18,0,0,0"
                            Text="!!!"
                            Visibility="{Binding ElementName=ControlWithError, Path=AdornedElement.(Validation.HasError),Converter={StaticResource BoolToHiddenOrVisibleConverter}}"   >
                </TextBlock>
            </DockPanel>
        </ControlTemplate>
    </Setter.Value>
</Setter>