WPF:Validation.ErrorTemplate在隐藏装饰控件(TextBox)时不会隐藏

时间:2010-10-07 15:11:35

标签: c# wpf adorner

我有一个隐藏的TextBox,具体取决于是否在ComboBox中选择了一个项目。

这部分工作正常。

但是,它也设置了ValidatesOnDataErrors,如果TextBox存在错误,那么当TextBox被隐藏时,ErrorTemplate(在Adorner层中)仍然存在。

我想我明白,因为ErrorTemplate被设置到全局Adorner层中,所以它没有意识到它没有逻辑连接的TextBlock已被隐藏。

有关如何使用或围绕此工作的任何想法?我尝试在Grid中添加一个显式的AdornerDecorator,它绑定到ComboBox值。

1 个答案:

答案 0 :(得分:8)

您显然可以将AdornerElementPlaceholder的可见性绑定到装饰器本身的可见性。这是我的代码:

<ControlTemplate x:Key="EmptyErrorTemplate">
    <Border Background="Transparent" BorderBrush="Transparent" BorderThickness="0" IsHitTestVisible="false"
            Visibility="{Binding ElementName=placeholder, Path=AdornedElement.Visibility}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Margin="0,0,-30,0" Text="!" 
                       Foreground="Red"
                       FontSize="34"
                       VerticalAlignment="Center"/> 
            <AdornedElementPlaceholder Name="placeholder" />
        </StackPanel>
    </Border>
</ControlTemplate>