2009-09-16 68 views
2

时,我已经有了一个TextBox风格展现一个校验错误消息如下:在ExpanderWPF扩展仍然显示验证错误装饰器缩水

<Style TargetType="{x:Type TextBox}"> 
     <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> 
     <Setter Property="Validation.ErrorTemplate"> 
      <Setter.Value> 
       <ControlTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <Border BorderBrush="{Binding Path=ErrorContent, 
           Converter={StaticResource ValidationErrorToBrushConverter}}" BorderThickness="2"> 
          <AdornedElementPlaceholder /> 
         </Border> 
         <Image Name="image1" Height="14" Width="14" Stretch="Fill" Margin="1,1,1,1" 
           Source="{Binding Path=ErrorContent, 
           Converter={StaticResource ValidationErrorToImageSourceConverter}}" 
           ToolTip="{Binding Path=ErrorContent}"/> 
        </StackPanel> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

TextBox生活。当我打开Expander时,TextBox允许输入,但如果输入是NullorEmpty或包含特殊字符,则验证失败。

我的问题是,当我触发验证错误时,TextBox以红色点亮并显示一个图标,并显示消息作为工具提示。迄今为止都很好。但是当我没有通过验证关闭Expander时,带有工具提示的红色轮廓和图标仍然存在!即使与Expander缩小!只是漂浮在那里......这不是好行为。

关于如何让验证内容隐藏以及Expander中所有其他控件的任何想法?此外,用于验证的样式在UserControl的资源中声明,而不是在Expander本身中。

+0

检查[这个答案](http://stackoverflow.com/questions/1471451/wpf-error解决同样的问题-template-red-box-still-visible-on-collapse-of-an-expander/1471733#1471733)或[this answer](http://stackoverflow.com/questions/321327/how-do-i-get (当我的wpf-binding-validation-has-failed-a/321987#321987)的可能解决方案。这两个中的任何一个都适合你。 – 2009-10-19 10:23:29

回答

0

我最终在关闭扩展器时清除了TextBox。这样,验证错误就消失了,当Expander再次打开时,该框已清除并准备好进行另一次输入。

0

我有同样的问题。我通过将AdornerDecorator作为扩展器的第一个子对象来修复它。 AdornerDecorator在Expander崩溃时会崩溃,所以Adorner也应该消失。

0

我已经通过在文本框隐藏在Validation.ErrorTemplate属性设置为null

<Style TargetType="TextBox"> 
    <Style.Triggers> 
     <Trigger Property="IsHitTestVisible" Value="False"> 
      <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style>