2010-06-23 114 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