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找到它,但我们仍然无法设置工具提示。

alt text

在TemplatedAdorner可见这里的唯一的事情就是边界。边界知道它的孩子 - TemplatedAdorner - 它知道它的AdornedElement - TextBox。所以我们可以用这个设置边框的工具提示。 (然而,这种结合似乎无法更新工具提示的边框。当我看着它探听之后,它会显示它的工作原理。)

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

所以,文本框有其AttachedProperty验证,我们可以找到ErrorContent,因此它必须像你在最后一个例子中那样设置自己的ToolTip,否则它将无法工作。

+2

解决方案是好的,但要避免在例外的Output.Debug的窗户,我会想办法让你的工具提示工具提示绑定=“{绑定的RelativeSource = {的RelativeSource自}, 路径= Child.AdornedElement。(Validation.Errors ).CurrentItem.ErrorContent}“ – Dude0001 2014-01-10 16:13:38

我知道我迟到了,但让我分享一个解决方案,我发现研究这个问题:WPF custom validator with tooltip

这是最简单的形式,此ErrorTemplate只显示Tooltip与整个AdornedElementErrorContent

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

将这个TemplateResources,所有你需要做的是设置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> 
+1

这是一个好主意;不幸的是,您的第一个解决方案会在整个文本框中放置一个(透明)叠加层,以防止用户将其聚焦并纠正错误。 – poke 2013-09-10 13:40:15

+0

@poke你说得对。第一种解决方案仅适用于显示错误的控件。但也许第二个解决方案是无论如何你想要的。 – LPL 2013-09-10 14:56:41

+0

第二种解决方案适用于TextBox,但不适用于ComboBox。如果我将IsHitTestVisible设置为true,我完全无法点击组合框,这似乎是为了让ToolTip正常工作所必需的。有什么想法吗? – 2014-04-07 15:36:07