2016-03-04 56 views
2

我已將IDataErrorInfo應用於我的自定義控件'ViewModel s。一切工作正常(邊框被繪成紅色,提示錯誤的工具提示是肖恩),但我想知道是否有辦法有兩個不同的錯誤和警告Validation.ErrorTemplateIDateErrorInfo Multi Validation.ErrorTemplate

我的自定義風格控制(與Validation.ErrorTemplate)

 <Style TargetType="{x:Type controls:CustomTextBoxNumeric}"> 
     <Setter Property="TextAlignment" Value="Right"/> 
     <Setter Property="Validation.ErrorTemplate"> 
      <Setter.Value> 
       <ControlTemplate> 
        <DockPanel LastChildFill="True"> 
         <Border BorderBrush="Red" BorderThickness="1"> 
          <AdornedElementPlaceholder /> 
         </Border> 
        </DockPanel> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="true"> 
       <Setter Property="ToolTip" 
         Value="{Binding RelativeSource={RelativeSource Self}, 
         Path=(Validation.Errors).CurrentItem.ErrorContent}"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

我的自定義控制視圖模型

public class CustomTextBoxNumericViewModel : BaseComponentViewModel, IDataErrorInfo 
{ 
    private decimal? decimalValue; 
    private bool hasErrors; 
    private bool hasWarnings; 

    public CustomTextBoxNumericViewModel() 
    { 

    } 

    [DataMember(EmitDefaultValue = false)] 
    public decimal? DecimalValue 
    { 
     get { return this.decimalValue; } 
     set { this.decimalValue = value; this.Changed("DecimalValue"); this.Changed("HasErrors"); } 
    } 

    [DataMember(EmitDefaultValue = false)] 
    public bool HasErrors 
    { 
     get { return this.hasErrors; } 
     set { this.hasErrors = value; this.Changed("HasErrors"); this.Changed("DecimalValue"); } 
    } 

    [DataMember(EmitDefaultValue = false)] 
    public bool HasWarnings 
    { 
     get { return this.hasWarnings; } 
     set { this.hasWarnings = value; this.Changed("HasWarnings"); this.Changed("DecimalValue"); } 
    } 

    #region IDataErrorInfo Implementation 

    public string Error 
    { 
     get 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    public string this[string propertyName] 
    { 
     get 
     { 
      if (propertyName == "DecimalValue") 
      { 
       if (HasErrors) 
       { 
        return this.ErrorsField; 
       } 
       if (HasWarnings) 
       { 
        return this.WarningsField; 
       } 
       if (DecimalValue < 0) 
       { 
        return "Must be greater than 0"; 
       } 
      } 
      return string.Empty; 
     } 
    } 

    #endregion 
} 
+0

您可以通過使用Style.Triggers達到它 –

回答

1

我設法解決(INotifyPropertyChanged的在基視圖模型實現)我的問題使用ControlTemplate資源。

我的風格改爲:

<Style TargetType="{x:Type controls:CustomTextBoxNumeric}"> 
     <Setter Property="TextAlignment" Value="Right"/> 
     <Setter Property="Validation.ErrorTemplate"> 
      <Setter.Value> 
       <ControlTemplate> 
        <DockPanel LastChildFill="True"> 
         <Border BorderBrush="Red" BorderThickness="1"> 
          <AdornedElementPlaceholder /> 
         </Border> 
        </DockPanel> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="true"> 
       <Setter Property="ToolTip" 
         Value="{Binding RelativeSource={RelativeSource Self}, 
         Path=(Validation.Errors).CurrentItem.ErrorContent}"/> 
      </Trigger> 
      <DataTrigger Binding="{Binding Path=ViewModel.HasWarnings, RelativeSource={RelativeSource Self}}" Value="True"> 
       <Setter Property="Validation.ErrorTemplate" Value="{DynamicResource EntypoWarningTemplate}" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding Path=ViewModel.HasErrors, RelativeSource={RelativeSource Self}}" Value="True"> 
       <Setter Property="Validation.ErrorTemplate" Value="{DynamicResource EntypoErrorTemplate}" /> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 

ControlTemplates

 <ControlTemplate x:Key="MyErrorTemplate" TargetType="{x:Type Control}"> 
     <DockPanel LastChildFill="True"> 
      <Border BorderBrush="Red" BorderThickness="1"> 
       <AdornedElementPlaceholder /> 
      </Border> 
     </DockPanel> 
    </ControlTemplate> 

    <ControlTemplate x:Key="MyWarningTemplate" TargetType="{x:Type Control}"> 
     <DockPanel LastChildFill="True"> 
      <Border BorderBrush="Orange" BorderThickness="1"> 
       <AdornedElementPlaceholder /> 
      </Border> 
     </DockPanel> 
    </ControlTemplate>