2016-03-04 57 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>