【问题标题】:Validation ControlTemplate string always appearing even when no Validation Error即使没有验证错误,验证 ControlTemplate 字符串也始终出现
【发布时间】:2014-04-04 15:34:26
【问题描述】:

我创建了一个样式,以指定在某些文本框中出现验证错误时要使用的验证 ControlTemplate。验证错误被捕获,因为我可以看到正在使用我的样式(文本框的默认红色边框,以及我添加的带有红色“!!!”字符串的浅粉色背景),但问题是“!!!”红色字符串始终存在,即使没有验证错误(尽管粉红色背景和红色边框消失)。我正在使用 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 TextBox 并添加了一些功能(因为我使用的是自定义框架):

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

【问题讨论】:

  • 你试过用Source={RelativeSource...代替RelativeSource={RelativeSource....
  • 一种快速的方法...只需将其可见性绑定到已经正确切换的东西。
  • @verdesrobert 您能否发布 Source={RelativeSource... 的完整语法,因为我不知道。在 WPF 方面,我是一个初学者,随着我在这个项目中的推进,我开始学习
  • 刚刚复制了你的代码并做了一点改动Visibility="{Binding Validation.HasError, Source={RelativeSource Self}, Converter={StaticResource BoolToHiddenOrVisibleConverter}}"
  • 肯定在 Textblock Visibility="{Binding ElementName=ControlWithError, Path=AdornedElement.(Validation.HasError),Converter={StaticResource CustomConverter}}">

标签: c# wpf validation xaml


【解决方案1】:

这肯定有效

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

【讨论】:

  • 这真的适用于 DataGridROW 吗?或只是 jor 文本框? (我有同样的问题,但与行有关。在纠错后正确渲染单元格效果很好,但之后行保持红色边框和 ControlTemplate 中的“!”。
  • BoolToHiddenOrVisibleConverter 在做什么......它看起来怎么样??
  • 它根据传递的布尔值返回 Visibility.Hidden 或 Visibility.Visible
猜你喜欢
  • 1970-01-01
  • 2017-12-24
  • 2017-10-18
  • 2017-08-02
  • 2018-12-23
  • 2019-06-11
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
相关资源
最近更新 更多