【问题标题】:Trigger validation on a label with WPF + Catel使用 WPF + Catel 在标签上触发验证
【发布时间】:2017-10-10 20:44:44
【问题描述】:

我正在尝试通过 Property="Validation.ErrorTemplate" 在 WPF 标签上设置样式验证。问题是即使是标准验证也不会触发。我的目标是将文本的前景变为红色。

<Label Content="{Binding LabelConformidadValidadion, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"></Label>

我使用的是 CATEL 4.5.2,并使用以下代码设置验证。

protected override void ValidateFields(List<IFieldValidationResult> validationResults)
        {
            if (Peso!=null && !Peso.Peso_Caliente.HasValue)
                validationResults.Add(FieldValidationResult.CreateErrorWithTag(Peso_CalienteProperty,"No se ha capturado el peso", "Captura_PesoCalienteCanExecute"));
            if (Peso!=null && !Peso.IC.HasValue)
                validationResults.Add(FieldValidationResult.CreateErrorWithTag(LabelConformidadValidadion, "No se ha capturado el indicador IC", "Captura_PesoCalienteCanExecute"));
        }

视图模型已验证,但标签周围的标准红色框从未出现。我发现的一件事是,如果我正在调试并打开和关闭标签上的 NotifyOnValidationError=True 属性,标签确实会显示通常的红色框。

【问题讨论】:

  • 您是否也尝试设置 ValidatesOnDataErrors=true?
  • 我做到了,两者都打开了。然后单独设置每一个。

标签: c# wpf validation catel


【解决方案1】:

问题是我将规则附加到字段而不是 PropertyData。

完整答案是这样的

XAML。

<Label Content="{Binding LabelConformidadValidadion, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}">
                                    <Label.Style>
                                        <Style TargetType="Label">
                                            <Style.Triggers>
                                                <Trigger Property="Validation.HasError" Value="true">
                                                    <Setter Property="Foreground" Value="Red"></Setter>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Label.Style>
                                </Label>

C#:

public string LabelConformidadValidadion
        {
            get { return GetValue<string>(LabelConformidadValidadionProperty); }
            set { SetValue(LabelConformidadValidadionProperty,value); }
        }

        public static readonly PropertyData LabelConformidadValidadionProperty = RegisterProperty("LabelConformidadValidadion", typeof(string), null);

...

protected override void ValidateFields(List<IFieldValidationResult> validationResults)
        {
            if (Peso!=null && !Peso.Peso_Caliente.HasValue)
                validationResults.Add(FieldValidationResult.CreateErrorWithTag(Peso_CalienteProperty,"No se ha capturado el peso", "Captura_PesoCalienteCanExecute"));
            if (Peso!=null && !Peso.IC.HasValue)
                validationResults.Add(FieldValidationResult.CreateError(LabelConformidadValidadionProperty, "No se ha capturado el indicador IC"));
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多