[英]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.