WPF验证错误:设置错误消息的工具提示
为什么没有错误提示文本?WPF验证错误:设置错误消息的工具提示
<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}" />
失败,但低于suceeds,即使采用相同的结合,这是为什么呢? 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>
不能放在AdornedElementPlaceholder一个提示,我不认为这是明显的,在所有的,它只是为保留谁使用它(在你的情况下,一个TextBox)空间。用Snoop查看可视树,我们可以看到,TemplatedAdorner最终在VisualTree中的不同位置处,而不是TextBox,因此我们现在可以从VisualTree中找到TextBox。我们可以通过AdornedElement找到它,但我们仍然无法设置工具提示。
在TemplatedAdorner可见这里的唯一的事情就是边界。边界知道它的孩子 - TemplatedAdorner - 它知道它的AdornedElement - TextBox。所以我们可以用这个设置边框的工具提示。 (然而,这种结合似乎无法更新工具提示的边框。当我看着它探听之后,它会显示它的工作原理。)
<Border BorderBrush="Red"
BorderThickness="4"
ToolTip="{Binding RelativeSource={RelativeSource self},
Path=Child.AdornedElement.(Validation.Errors)[0].ErrorContent}">
所以,文本框有其AttachedProperty验证,我们可以找到ErrorContent,因此它必须像你在最后一个例子中那样设置自己的ToolTip,否则它将无法工作。
我知道我迟到了,但让我分享一个解决方案,我发现研究这个问题:WPF custom validator with tooltip。
这是最简单的形式,此ErrorTemplate
只显示Tooltip
与整个AdornedElement
的ErrorContent
。
<ControlTemplate x:Key="validationTemplate">
<Grid Background="Transparent"
ToolTip="{Binding Path=/ErrorContent}">
<AdornedElementPlaceholder />
</Grid>
</ControlTemplate>
但是,当然你可以根据需要装饰它,与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>
将这个Template
在Resources
,所有你需要做的是设置Validation.ErrorTemplate
。
Validation.ErrorTemplate="{StaticResource validationTemplate}"
即使这个恼人的触发器已不再需要。
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
解决方案是好的,但要避免在例外的Output.Debug的窗户,我会想办法让你的工具提示工具提示绑定=“{绑定的RelativeSource = {的RelativeSource自}, 路径= Child.AdornedElement。(Validation.Errors ).CurrentItem.ErrorContent}“ – Dude0001 2014-01-10 16:13:38