IDataErrorInfo 多 Validation.ErrorTemplate

IDateErrorInfo Multi Validation.ErrorTemplate

我已经对我的自定义控件 ViewModel 实施了 IDataErrorInfo。一切正常(边框被绘制为红色,带有错误的工具提示正在显示),但我想知道是否有办法为错误和警告设置两个不同的 Validation.ErrorTemplates。

我的自定义控件样式(带有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>

我的自定义控件 ViewModel(INotifyPropertyChanged 在基础 ViewModel 中实现)

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
}

我设法使用 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>