WPF 验证错误:设置带有错误消息的工具提示 [英] WPF Validation Errors: Setting Tooltip with Error Message

查看:36
本文介绍了WPF 验证错误:设置带有错误消息的工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么没有关于错误的工具提示文本?

Why is there no tooltip text on errors?

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <StackPanel>
                    <Border ...>
                        <AdornedElementPlaceholder ... 
                            ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                    </Border>
                    ...
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我也注意到了

<AdornedElementPlaceholder ...
    ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />

失败但下面的成功,即使具有相同的绑定,为什么会这样?AdornedElementPlaceholder 不是指文本框吗?即使没有,工具提示不应该出现在某处吗?

fails but the below suceeds, even with the same binding, why is this so? Doesn't AdornedElementPlaceholder refer to the text box? Even if it doesn't, shouldn't a tooltip appear somewhere?

<Style.Triggers>
    <Trigger Property="Validation.HasError" Value="True">
        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
    </Trigger>
</Style.Triggers>

推荐答案

你不能在 AdornedElementPlaceholder 上放置工具提示,我认为它根本不可见,它只是为使用它的人保留空间(在你的情况下一个文本框).使用 Snoop 查看可视化树,我们可以看到 TemplatedAdorner 在可视化树中的最终位置与文本框不同,因此我们现在可以从可视化树中找到文本框.我们可以通过 AdornedElement 找到它,但是我们仍然无法设置工具提示.

You can't place a tooltip on the AdornedElementPlaceholder, I don't think it's visible at all, it's just reserving space for whoever uses it (in your case a TextBox). Looking at the Visual Tree with Snoop we can see that the TemplatedAdorner ends up in a different place in the VisualTree than the TextBox so there will be now way for us to find the TextBox from the VisualTree. We can find it through AdornedElement, but we still won't be able to set a tooltip.

在 TemplatedAdorner 中唯一可见的是边框.Border 知道它的 Child - TemplatedAdorner - 而后者又知道它的 AdornedElement - TextBox.所以我们可以用这个设置边框的工具提示.(但是,此绑定似乎无法更新边框的工具提示.当我使用 Snoop 查看它时它会起作用,然后显示.)

The only thing visible here in the TemplatedAdorner is the Border. The Border knows its Child - the TemplatedAdorner - which in turn knows its AdornedElement - the TextBox. So we could set the ToolTip for the Border with this. (However, this Binding seems to fail to update the Tooltip for the Border. It works when I look at it with Snoop and after that it displays.)

<Border BorderBrush="Red"
        BorderThickness="4"
        ToolTip="{Binding RelativeSource={RelativeSource self},
                  Path=Child.AdornedElement.(Validation.Errors)[0].ErrorContent}">

因此,TextBox 有它的 AttachedProperty 验证,我们可以在其中找到 ErrorContent,因此它必须像您在上一个示例中所做的那样设置自己的 ToolTip,否则它将无法工作.

So, the TextBox has its AttachedProperty Validation where we can find the ErrorContent so it must set its own ToolTip like you did at your last example, otherwise it won't work.

这篇关于WPF 验证错误:设置带有错误消息的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆