首页 文章

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

提问于
浏览
1

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

这部分工作正常 .

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

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

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

1 回答

  • 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>
    

相关问题