Validation.ErrorTemplate样式问题

时间:2010-06-23 01:32:40

标签: wpf

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

以下样式有效:

     <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有更简洁的方法吗?

1 个答案:

答案 0 :(得分:22)

ErrorTemplate是adorner,不是控件模板的替代品。您在模板中包含AdornedElementPlaceholder,您希望为原始控件留出空间。因此,您的错误模板应如下所示:

<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>