Wpf Validation.ErrorTemplate样式问题

Wpf Validation.ErrorTemplate样式问题,wpf,Wpf,我试图在内容无效时将样式应用于文本框 以下风格作品: <Style x:Key="textBoxNormalStyle" TargetType="{x:Type TextBox}"> <Setter Property="FontFamily" Value="Arial"/> <Setter Property="FontSize" Value="16"/> <Setter Property="ForceCursor" Va

我试图在内容无效时将样式应用于文本框

以下风格作品:

     <Style x:Key="textBoxNormalStyle" TargetType="{x:Type TextBox}">
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="ForceCursor" Value="False"/>
    <Setter Property="Foreground" Value="{StaticResource TextColor}"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="3,0,3,0"/>
    <Setter Property="Margin" Value="2"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Background="{StaticResource TextBackColor}" BorderBrush="{StaticResource BorderColor}" x:Name="Bd" CornerRadius="4" BorderThickness="2">
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
<Setter Property="Validation.ErrorTemplate">
  <Setter.Value>
    <ControlTemplate>
    </ControlTemplate>
  </Setter.Value>
</Setter>
<Style.Triggers>
  <Trigger Property="Validation.HasError" Value="True">
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="ForceCursor" Value="False"/>
    <Setter Property="Foreground" Value="{StaticResource TextColor}"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="3,0,3,0"/>
    <Setter Property="Margin" Value="2"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type TextBox}">
          <Grid Name="test">
            <Border Background="{StaticResource TextBackColor}" BorderBrush="#d99" x:Name="Bd" CornerRadius="4" BorderThickness="2">
              <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
            </Border>
            <Image Name="ErrorImage" Width="24" Height="24" Margin="0,0,4,0" Source="/Images/error.png" HorizontalAlignment="Right">
            </Image>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Trigger>
</Style.Triggers>

但是,我想去掉上面的触发器,并将触发器中的控件模板移动到Validation.ErrorTemplate部分。但当我执行此操作时,错误模板的控件无法正确显示(错误模板的大小默认为其中包含的图像的大小,左对齐,并阻止用户在文本框中键入的文本)

上面的xaml有更干净的方法吗?

ErrorTemplate是一个,而不是控件模板的替代品。在模板中包含要为原始控件留出空间的。因此,您的错误模板应该如下所示:

<Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate>
            <Grid Name="test">
                <AdornedElementPlaceholder/>
                <Image Name="ErrorImage" Width="24" Height="24" Margin="0,0,4,0" Source="/Images/error.png" HorizontalAlignment="Right"/>
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

由于您还希望更改边框的颜色,因此我将为边框笔刷使用TemplateBinding,并在Validation.HasError触发器中更改它:

<Setter Property="BorderBrush" Value="{StaticResource BorderColor}"/>
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type TextBox}">
            <Border Background="{StaticResource TextBackColor}" BorderBrush="{TemplateBinding BorderBrush}" x:Name="Bd" CornerRadius="4" BorderThickness="2">
                <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>
<Style.Triggers>
    <Trigger Property="Validation.HasError" Value="True">
        <Setter Property="BorderBrush" Value="#d99"/>
    </Trigger>
</Style.Triggers>

谢谢你,迈斯特。我能够将ControlTemplate移动到Validation.ErrorTemplate部分。xaml看起来绝对干净。出于某种原因,Validation.HasError触发器不会更改BorderBrush,因此我所做的是用红色的包装边框包装装饰占位符。