【问题标题】:Display error validation on another control在另一个控件上显示错误验证
【发布时间】:2016-12-03 08:28:34
【问题描述】:

我有一个TextBlock 和一个CheckBox,例如:

<StackPanel >
    <TextBlock Text="Colors"/>
    <CheckBox Content="Blue" IsChecked="{Binding Model.Blue, ValidatesOnNotifyDataErrors=False}"/>
</StackPanel>

在我的模型中,我正在实现INotifyDataErrorInfo 并验证复选框是否被选中。如果没有检查,我将其视为错误:

public class MyModel : INotifyPropertyChanged, INotifyDataErrorInfo
{
    [CustomValidation(typeof(MyModel), "CheckBoxRequired")]
    public bool Blue
    {
        get { return _blue; }
        set { _blue = value; RaisePropertyChanged(nameof(Blue)); }
    }

    public static ValidationResult CheckBoxRequired(object obj, ValidationContext context)
    {
        var model = (MyModel)context.ObjectInstance;
        if (model.Blue == false)
            return new ValidationResult("Blue required", new string[] { "Blue" });
        else
            return ValidationResult.Success;
    }

    //...
    //INotifyPropertyChanged & INotifyDataErrorInfo implementations omitted
}

当我将ValidatesOnNotifyDataErrors 设置为true 时,它会在CheckBox 周围正确显示一个红色框。它看起来像这样:

我不希望出现红色复选框。为此,我明确地将ValidatesOnNotifyDataErrors 设置为false。这很好用。

出现错误时我想做的就是在TextBlock上显示错误,比如更改TextBlock的字体颜色。 TextBlock 如何知道CheckBox 上存在的任何错误,最好的解决方法是什么?

我的预期结果是这样的:

【问题讨论】:

    标签: c# wpf validation xaml inotifydataerrorinfo


    【解决方案1】:

    首先设置ValidatesOnNotifyDataErrors不是摆脱红色边框的正确方法。这将导致您的数据根本无法验证。你想要的是这样的:

    <CheckBox Content="Blue" IsChecked="{Binding Model.Blue, ValidatesOnNotifyDataErrors=True}" Validation.ErrorTemplate="{x:Null}"/>
    

    其次,为了得到想要的结果,我会使用this 的方法。您可以使用触发器来了解 CheckBox 中是否存在错误(ErrorsChanged 事件和HasError 属性在这里应该很有用)并设置 TextControl 的文本颜色。

    以下是完成此操作的代码:

    <TextBlock Text="Color">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=checkBox, Path=(Validation.HasError)}" Value="True">
                        <Setter Property="Foreground" Value="Red" />
                        <Setter Property="ToolTip" Value="{Binding ElementName=checkBox, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    <CheckBox x:Name="checkBox"
              Margin="4,0"
              Content="Blue"
              IsChecked="{Binding Model.Blue}"
              Validation.ErrorTemplate="{x:Null}" />
    

    【讨论】:

    • 完美运行,谢谢。我已经用我使用的代码发布了一个答案,以便任何可能有相同问题的人都可以参考它。
    • 为了整合信息,我建议对 Karina 的帖子进行编辑,以包含您提出的特定代码。
    • 我认为我以前没有见过在标记扩展中使用括号,例如Path=(Validation.HasError)。那是为了什么?
    【解决方案2】:

    凭借 Karina K 的洞察力,我使用以下代码来达到预期的效果:

    <TextBlock Text="Color">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=checkBox, Path=(Validation.HasError)}" Value="True">
                        <Setter Property="Foreground" Value="Red" />
                        <Setter Property="ToolTip" Value="{Binding ElementName=checkBox, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    <CheckBox x:Name="checkBox"
              Margin="4,0"
              Content="Blue"
              IsChecked="{Binding Model.Blue}"
              Validation.ErrorTemplate="{x:Null}" />
    

    【讨论】:

    • 为了整合信息,我建议对 Karina 的帖子进行编辑,以包含您提出的特定代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2013-04-07
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    相关资源
    最近更新 更多