2014-09-24 31 views
1

我有一個自定義Validation.ErrorTemplate,由於某種原因,WPF顯示了我的自定義錯誤模板和默認模板。他們都顯示相同的錯誤,但我不想顯示默認的ErrorTemplate。默認ErrorTemplate與自定義ErrorTemplate一起顯示

我的代碼:

<Style TargetType="TextBox" x:Key="MyTextBox"> 
    <Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Grid> 
        <Border BorderBrush="red" BorderThickness="1" Background="#11FF0000" IsHitTestVisible="False" x:Name="errorBorder"/> 
        <AdornedElementPlaceholder x:Name="placeholder" /> 
        <Popup AllowsTransparency="True" HorizontalAlignment="Right" HorizontalOffset="0" VerticalOffset="0" PopupAnimation="Fade" Placement="Right" 
           PlacementTarget="{Binding ElementName=errorBorder}" IsOpen="{Binding ElementName=placeholder, Path=AdornedElement.IsFocused, Mode=OneWay}"> 
         <StackPanel Orientation="Horizontal"> 
          <Polygon VerticalAlignment="Center" Points="0,4,4,4" Fill="red" Stretch="Fill" Stroke="red" 
            StrokeThickness="2" /> 
          <Border Background="red" CornerRadius="0" Padding="4"> 
           <TextBlock HorizontalAlignment="Center" Foreground="white" FontWeight="Bold" Margin="2,0,0,0" 
               Text="{Binding ElementName=placeholder, Path=AdornedElement.ToolTip, Mode=OneWay}" /> 
          </Border> 
         </StackPanel> 
        </Popup> 
       </Grid> 
      </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}" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

<Style TargetType="TextBox" BasedOn="{StaticResource MyTextBox}"/> 

我想知道是否有人知道爲什麼WPF顯示我的兩個錯誤模板和默認的。

編輯 http://i58.tinypic.com/a14k6q.png - 與這兩個錯誤畫面顯示

+0

來自你的風采刪除觸發'Validation.HasError'。 – Bolu 2014-09-24 13:27:39

回答

0

這是因爲你定義ErrorTemplate也defiend一個Validation.HasError觸發在你的風格,你可以使用其中之一。如果你想使用ErrorTemplate您需要刪除的觸發器,更改文本結合"Path=AdornedElement.Validation.Errors).CurrentItem.ErrorContent"那麼你只會看到ErrorTemplate結果:

<Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Grid> 
        <Border/> 
        <AdornedElementPlaceholder x:Name="placeholder" /> 
        <Popup> 
         <StackPanel> 
          <Polygon/> 
          <Border> 

      <TextBlock Text="{Binding ElementName=placeholder, 
         Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent, 
         Mode=OneWay}" /> 
          </Border> 
         </StackPanel> 
        </Popup> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
+0

謝謝!那就是訣竅。 – Timeless 2014-09-24 13:39:42

+0

@無時間,請注意您可以使用其中任何一個來顯示錯誤消息。只是不適用這兩個。 – Bolu 2014-09-24 13:43:04

相關問題