[英]Trigger validation on a label with WPF + Catel
我正在尝试通过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属性,则该标签确实会显示通常的红色框。
问题是我将规则附加到字段而不是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"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.