WPF 在控件下方显示带有验证错误消息的 TextBlock

标签 wpf wpf-controls binding

有没有办法在控件下方的 TextBlock 中显示错误内容,类似于以下设置工具提示以包含错误文本的方式?

        <Style x:Key="textBoxInError" TargetType="Control">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel>
                        <TextBlock DockPanel.Dock="Left" Foreground="Red" FontWeight="Bold">*</TextBlock>
                        <TextBlock Text="WOULD LIKE TO SHOW WHAT TOOLTIP IS SHOWING" DockPanel.Dock="Bottom" Foreground="Red"/>
                        <Border BorderBrush="Red" BorderThickness="2">
                            <AdornedElementPlaceholder/>
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <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>
    </Style>

换句话说,我宁愿在控件下方的 TextBlock 中而不是在工具提示中显示错误消息。

最佳答案

ErrorTemplate 的 DataContext 已经是 Validation.Errors 的值,所以你可以这样做:

<TextBlock Text="{Binding [0].ErrorContent}" DockPanel.Dock="Bottom" Foreground="Red"/>

要么
<TextBlock Text="{Binding ErrorContent}" DockPanel.Dock="Bottom" Foreground="Red"/>

关于WPF 在控件下方显示带有验证错误消息的 TextBlock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3362563/

相关文章:

wpf - XAML 绑定(bind)到数据对象的父级

wpf - WPF文本框字符串格式的奇怪问题 - 光标向后移动

c# - WPF 应用程序中的多个数据网格,但一次只显示一个

c# - WPF 仪表板覆盖第 3 方应用程序

c# - 有没有办法解决Expression Blend的 "extract"WPF控件?

binding - 无法在 Screen 的 C-A Esc 模式下将 HJKL 绑定(bind)到 DLTH

当 ScrollBar 可见时,WPF ScrollViewer 显示边框

c# - 自定义 WPF 中列表框中选择的可视化

wpf - 有没有办法重新使用已经关闭的 WPF 窗口实例

wpf - 焦点未正确进入目标用户控件