【问题标题】:WPF Display TextBlock with validation error message below controlWPF 在控件下方显示带有验证错误消息的 TextBlock
【发布时间】:2011-03-22 17:04:39
【问题描述】:

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

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

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

【问题讨论】:

  • 我遇到的问题是错误消息文本块将呈现在可能位于其下方的其他控件之上。它不会把所有东西都撞下来为它腾出空间。与装饰层上显示的错误模板有关...尚未完全弄清楚。

标签: wpf wpf-controls binding


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    相关资源
    最近更新 更多