简体   繁体   English

使用WPF + Catel在标签上触发验证

[英]Trigger validation on a label with WPF + Catel

I am trying to style validation on a WPF label via Property="Validation.ErrorTemplate". 我正在尝试通过Property =“ Validation.ErrorTemplate”对WPF标签进行样式验证。 Problem is that not even the standard validation triggers. 问题在于,甚至没有标准的验证触发器。 My objective is changing the foreground of the text to red. 我的目标是将文本的前景更改为红色。

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

Im using CATEL 4.5.2 and i set the validation with the following code. 我正在使用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"));
        }

The viewmodel is validated but the standard red box around the label never appears. 视图模型已验证,但标签周围的标准红色框从未显示。 One thing i did found out is that the label does show the usual red box if I am debugging and turning on and off the NotifyOnValidationError=True property on the label. 我发现的一件事是,如果我正在调试并打开和关闭标签上的NotifyOnValidationError = True属性,则该标签确实会显示通常的红色框。

Problem was that i attaching the rule to the field and not the PropertyData. 问题是我将规则附加到字段而不是PropertyData。

Full answer is this 完整的回答是这个

XAML. 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#: 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"));
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM