简体   繁体   中英

Trigger validation on a label with WPF + Catel

I am trying to style validation on a WPF label via Property="Validation.ErrorTemplate". 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.

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.

Problem was that i attaching the rule to the field and not the PropertyData.

Full answer is this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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