WPF 验证错误:设置带有错误消息的工具提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4161523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Validation Errors: Setting Tooltip with Error Message
提问by Jiew Meng
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>
I also noticed that
我也注意到
<AdornedElementPlaceholder ...
ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
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?
失败但下面的成功,即使具有相同的绑定,为什么会这样?不是AdornedElementPlaceholder
指文本框吗?即使没有,工具提示不应该出现在某处吗?
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
采纳答案by Fredrik Hedblad
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.
您不能在 AdornedElementPlaceholder 上放置工具提示,我认为它根本不可见,它只是为使用它的人保留空间(在您的情况下为 TextBox)。使用 Snoop 查看可视化树,我们可以看到 TemplatedAdorner 在可视化树中的最终位置与文本框不同,因此我们现在可以从可视化树中找到文本框。我们可以通过 AdornedElement 找到它,但是我们仍然无法设置工具提示。
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.)
TemplatedAdorner 中唯一可见的是边框。Border 知道它的 Child - TemplatedAdorner - 而后者又知道它的 AdornedElement - TextBox。所以我们可以用这个设置边框的工具提示。(但是,此绑定似乎无法更新边框的工具提示。当我使用 Snoop 查看它时它会起作用,然后它会显示出来。)
<Border BorderBrush="Red"
BorderThickness="4"
ToolTip="{Binding RelativeSource={RelativeSource self},
Path=Child.AdornedElement.(Validation.Errors)[0].ErrorContent}">
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.
因此,TextBox 有它的 AttachedProperty 验证,我们可以在其中找到 ErrorContent,因此它必须像您在上一个示例中所做的那样设置自己的 ToolTip,否则它将无法工作。
回答by LPL
I know I'm late, but let me share a solution I found studying this question: WPF custom validator with tooltip.
我知道我迟到了,但让我分享一个我在研究这个问题时发现的解决方案:WPF custom validator with tooltip。
In it's simplest form this ErrorTemplate
shows only a Tooltip
with the ErrorContent
for the whole AdornedElement
.
在最简单的形式中,这ErrorTemplate
仅显示了一个Tooltip
与ErrorContent
整体AdornedElement
。
<ControlTemplate x:Key="validationTemplate">
<Grid Background="Transparent"
ToolTip="{Binding Path=/ErrorContent}">
<AdornedElementPlaceholder />
</Grid>
</ControlTemplate>
But of course you can decorate it as desired e.g. with a Tooltip
for just a marker.
但当然,您可以根据需要装饰它,例如Tooltip
仅使用标记来装饰它。
<ControlTemplate x:Key="validationTemplate">
<Grid>
<Ellipse Fill="Red" Opacity="0.8" Width="10" Height="10"
HorizontalAlignment="Right" VerticalAlignment="Top"
ToolTip="{Binding Path=/ErrorContent}" />
<AdornedElementPlaceholder />
</Grid>
</ControlTemplate>
Put this Template
in Resources
and all you have to do is setting the Validation.ErrorTemplate
.
把它放进Template
去Resources
,你所要做的就是设置Validation.ErrorTemplate
.
Validation.ErrorTemplate="{StaticResource validationTemplate}"
Even this annoying Trigger is no longer needed.
甚至不再需要这个烦人的触发器。
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>