wpf 错误模板 - 扩展器折叠时仍可见红色框 [英] wpf error template - red box still visible on collapse of an expander

查看:18
本文介绍了wpf 错误模板 - 扩展器折叠时仍可见红色框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对 Expander 内的 TextBox 的 DataSource 进行一些验证,发现一旦触发验证错误,如果我折叠 Expander,红色框会留在 TextBox 所在的位置.

I'm doing some validation on the DataSource of TextBox that's within an Expander and have found that once a validation error has been triggered, if I collapse the Expander, the red box stays where the TextBox would have been.

<Expander Header="Blah Blah Blah">
  <TextBox Name="TextBox"
           Validation.ErrorTemplate="{DynamicResource TextBoxErrorTemplate}"
           Text="{Binding Path=Blah,
                          UpdateSourceTrigger=PropertyChanged,
                          ValidatesOnDataErrors=True}" />
</Expander>

我试图通过将错误模板的可见性绑定到扩展器来解决这个问题,但是我认为绑定有问题.

I've tried to get round this by binding the visibility of the Error Template to the Expander, however I think there's something wrong with the binding.

<local:NotVisibleConverter x:Key="NotVisibleConverter" />

<ControlTemplate x:Key="TextBoxErrorTemplate">
  <DockPanel>
    <Border BorderBrush="Red" BorderThickness="2" 
            Visibility="{Binding Path=IsExpanded, 
                                 Converter={StaticResource NotVisibleConverter}, 
                                 RelativeSource={RelativeSource AncestorType=Expander}}" >
      <AdornedElementPlaceholder Name="MyAdorner" />
    </Border>
  </DockPanel>
  <ControlTemplate.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
        <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self}, 
                                Path=(Validation.Errors)[0].ErrorContent}"/>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

我想我的绑定出错了,有人可以让我重回正轨吗?或者,有人知道在扩展器折叠时仍然可见的 ErrorTemplate 的另一种解决方案吗?

I guess I've gone wrong with my binding, can someone put me back on track please? Alternatively does anyone know another solution to the ErrorTemplate still being visible on the collapse of an Expander?

推荐答案

与其进行任何绑定,不如在扩展器内部的元素周围放置一个 AdornerDecorator.你看,验证错误模板被放置在装饰层上,它显示在其他一切之上.这最终就是你的问题所在.即使你的文本框因为扩展器关闭而看不到,错误模板仍然在装饰层上.

Rather than doing any binding, you could place an AdornerDecorator around the elements inside of your expander. You see, the validation error template is placed on the adorner layer that way it shows up on top of everything else. That's ultimately what your problem is. Even though your text box is not visible because the expander is closed, the error template is still on the adorner layer.

我相信您可以使用以下 xaml 解决此问题:

I believe you can fix this with the following xaml:

<Expander Header="Blah Blah Blah">
   <AdornerDecorator>
      <TextBox Name="TextBox"
               Validation.ErrorTemplate="{DynamicResource TextBoxErrorTemplate}"
               Text="{Binding Path=Blah,
                              UpdateSourceTrigger=PropertyChanged,
                              ValidatesOnDataErrors=True}" />
   </AdornerDecorator>
</Expander>

这会创建一个专门用于扩展器内的装饰层.当扩展器关闭时,AdornerDecorator 也会被隐藏,它上面的所有内容也应该隐藏.

This creates an adorner layer specifically for within the expander. When the expander is closed the AdornerDecorator also gets hidden and so should everything on it.

这篇关于wpf 错误模板 - 扩展器折叠时仍可见红色框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆