WPF Multiple Validation.ErrorTemplate s取决于错误类型

WPF Multiple Validation.ErrorTemplate s取决于错误类型,wpf,Wpf,我刚开始使用WPF,我在玩mvvm和样式 <Setter Property="Validation.ErrorTemplate"> <Setter.Value> <ControlTemplate> <DockPanel LastChildFill="True"> <TextBlock DockPanel.Dock="Righ

我刚开始使用WPF,我在玩mvvm和样式

    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right"
                               Foreground="Red"
                               FontSize="14pt"
                               Margin="-15,0,0,0" 
                               FontWeight="Bold">*</TextBlock>
                    <AdornedElementPlaceholder Name="controlWithError"/>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"          Value="{Binding RelativeSource={x:Static RelativeSource.Self},          Path=(Validation.Errors)[0].ErrorContent}"/>
            <Setter Property="Background"       Value="{StaticResource ErrorBackgroundBrush}"/>
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="BorderBrush" Value="{StaticResource TextControlBorderBrush}" />
        </Trigger>
    </Style.Triggers>
</Style>
我的视图模型字段名绑定了一个文本框。 FirstName是必填字段,具有最大长度

    [Required(ErrorMessage = "Required")]
    [StringLength(20, ErrorMessage = "Too long")]
    public String FirstName
    {
        get { return firstName; }
        set
        {
            firstName = value;
            NotifyOfPropertyChange("FirstName");
            NotifyOfPropertyChange("CanSave");
        }
    }
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right"
                               Foreground="Red"
                               FontSize="14pt"
                               Margin="-15,0,0,0" 
                               FontWeight="Bold">*</TextBlock>
                    <AdornedElementPlaceholder Name="controlWithError"/>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"          Value="{Binding RelativeSource={x:Static RelativeSource.Self},          Path=(Validation.Errors)[0].ErrorContent}"/>
            <Setter Property="Background"       Value="{StaticResource ErrorBackgroundBrush}"/>
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="BorderBrush" Value="{StaticResource TextControlBorderBrush}" />
        </Trigger>
    </Style.Triggers>
</Style>
我添加了一些样式,以便在出现错误时显示

    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right"
                               Foreground="Red"
                               FontSize="14pt"
                               Margin="-15,0,0,0" 
                               FontWeight="Bold">*</TextBlock>
                    <AdornedElementPlaceholder Name="controlWithError"/>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"          Value="{Binding RelativeSource={x:Static RelativeSource.Self},          Path=(Validation.Errors)[0].ErrorContent}"/>
            <Setter Property="Background"       Value="{StaticResource ErrorBackgroundBrush}"/>
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="BorderBrush" Value="{StaticResource TextControlBorderBrush}" />
        </Trigger>
    </Style.Triggers>
</Style>
现在我有了一个格式很好的框,它有一个红色的背景,当出现错误时有一个红色的星号。当您开始键入时,这将被删除,然后当长度达到20时,再次出现错误,这将返回到validationtemplate

    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right"
                               Foreground="Red"
                               FontSize="14pt"
                               Margin="-15,0,0,0" 
                               FontWeight="Bold">*</TextBlock>
                    <AdornedElementPlaceholder Name="controlWithError"/>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"          Value="{Binding RelativeSource={x:Static RelativeSource.Self},          Path=(Validation.Errors)[0].ErrorContent}"/>
            <Setter Property="Background"       Value="{StaticResource ErrorBackgroundBrush}"/>
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="BorderBrush" Value="{StaticResource TextControlBorderBrush}" />
        </Trigger>
    </Style.Triggers>
</Style>
我的问题是,是否有人针对不同类型的错误实现了不同的模板

    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right"
                               Foreground="Red"
                               FontSize="14pt"
                               Margin="-15,0,0,0" 
                               FontWeight="Bold">*</TextBlock>
                    <AdornedElementPlaceholder Name="controlWithError"/>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"          Value="{Binding RelativeSource={x:Static RelativeSource.Self},          Path=(Validation.Errors)[0].ErrorContent}"/>
            <Setter Property="Background"       Value="{StaticResource ErrorBackgroundBrush}"/>
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="BorderBrush" Value="{StaticResource TextControlBorderBrush}" />
        </Trigger>
    </Style.Triggers>
</Style>
我想做的是为每一个属性,需要有一个红色星号开始-没有红色背景。然后,当他们输入了一些内容,并且所需的字段错误消失后,星号应该消失

    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right"
                               Foreground="Red"
                               FontSize="14pt"
                               Margin="-15,0,0,0" 
                               FontWeight="Bold">*</TextBlock>
                    <AdornedElementPlaceholder Name="controlWithError"/>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"          Value="{Binding RelativeSource={x:Static RelativeSource.Self},          Path=(Validation.Errors)[0].ErrorContent}"/>
            <Setter Property="Background"       Value="{StaticResource ErrorBackgroundBrush}"/>
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="BorderBrush" Value="{StaticResource TextControlBorderBrush}" />
        </Trigger>
    </Style.Triggers>
</Style>
但是validaiton的另一个错误可能会显示一个感叹号和一个红色背景

    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right"
                               Foreground="Red"
                               FontSize="14pt"
                               Margin="-15,0,0,0" 
                               FontWeight="Bold">*</TextBlock>
                    <AdornedElementPlaceholder Name="controlWithError"/>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"          Value="{Binding RelativeSource={x:Static RelativeSource.Self},          Path=(Validation.Errors)[0].ErrorContent}"/>
            <Setter Property="Background"       Value="{StaticResource ErrorBackgroundBrush}"/>
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="BorderBrush" Value="{StaticResource TextControlBorderBrush}" />
        </Trigger>
    </Style.Triggers>
</Style>
有人吗

    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right"
                               Foreground="Red"
                               FontSize="14pt"
                               Margin="-15,0,0,0" 
                               FontWeight="Bold">*</TextBlock>
                    <AdornedElementPlaceholder Name="controlWithError"/>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"          Value="{Binding RelativeSource={x:Static RelativeSource.Self},          Path=(Validation.Errors)[0].ErrorContent}"/>
            <Setter Property="Background"       Value="{StaticResource ErrorBackgroundBrush}"/>
            <Setter Property="BorderThickness" Value="1" />
            <Setter Property="BorderBrush" Value="{StaticResource TextControlBorderBrush}" />
        </Trigger>
    </Style.Triggers>
</Style>