2010-06-23 53 views
4

我尝试在内容无效时将样式应用于文本框。Validation.ErrorTemplate样式问题

以下风格的作品:

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

回答

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触发:

​​
+0

Thanks Quartermeister。我能够将ControlTemplate移动到Validation.ErrorTemplate部分。 xaml绝对看起来更干净。 由于某种原因,Validation.HasError触发器不会更改BorderBrush,所以我所做的是将AdornedPlaceholder换成红色的包装边框。 – GoalMaker 2010-06-23 04:08:45