c# - WPF:装饰控件(文本框)隐藏时不隐藏 Validation.ErrorTemplate

标签 c# wpf adorner

我有一个文本框,它根据是否在组合框中选择了一个项目而被隐藏。

这部分工作正常。

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

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

关于如何使用或解决此问题的任何想法?我尝试在 Grid 中添加一个显式的 AdornerDecorator,它绑定(bind)到 ComboBox 值。

最佳答案

您显然可以绑定(bind) 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>

关于c# - WPF:装饰控件(文本框)隐藏时不隐藏 Validation.ErrorTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3883092/

相关文章:

c# - 通过套接字编程在 C# 中动态字节数组 [List<byte> 不起作用]

c# - 绑定(bind)重定向 hell

c# - Visual Studio 2017 - 表达式交互发生了什么?

c# - WPF C# PreviewDrop/Drop 事件未触发(使用 dragadorner)

wpf - 验证错误模板装饰器被切入 ScrollViewer 中

c# - 是否可以在 C# 中使用 "implicit"泛型类型参数

C#简单的网格填充算法

c# - 从其他表单调用时,DataGridView.DataSource 返回空 DataTable

wpf - '{DependencyProperty.UnsetValue }' is not a valid value for property ' FocusVisualStyle'

wpf - 我应该使用 AdornerLayer 来避免在屏幕外剪裁我的装饰器吗?