2013-02-27 41 views
2

在我们的WPF应用程序,我们有我们用一致的方式有什么方法可以在ErrorTemplate中设置工具提示吗?

<ResourceDictionary> 
    <ControlTemplate x:Key="ErrorTemplate"> 
     <Border BorderThickness="1" BorderBrush="Red"> 
      <AdornedElementPlaceholder /> 
     </Border> 
    </ControlTemplate> 
</ResourceDictionary> 

别处在我们的应用程序显示错误时,控制可能会显示我们设置ErrorTemplate像这样

<TextBox Validation.ErrorTemplate="{DynamicResource ErrorTemplate}" /> 
错误共同控制模板

我现在想在这个错误模板中显示一个工具提示,但是在边框上设置工具提示属性并没有太大的帮助,因为工具提示只在用户鼠标悬停在1px宽边框上时才显示,而不是控件本身是错误的。

我知道我可以在样式中设置工具提示,但是这个错误模板应用于许多不同的控件(组合框等),并且这些控件中的许多控件也使用独立于我的错误模板的样式 - 我真的希望能够以通用的方式将我的错误模板应用于任何控件。

有什么方法可以在我的ErrorTemplate中设置工具提示?

+0

相当肯定,这不能做 - 我见过的应用样式在一个通用的方式做到这一点的最好办法解释[这篇文章] (http://blogs.msdn.com/b/bethmassi/archive/2008/06/27/displaying-data-validation-messages-in-wpf.aspx) – Justin 2013-02-28 10:21:48

回答

1

我有一个定义的样式。我的对象(Customer)上有IDataErrorInfo,它对属性(LastName)进行了验证,该属性是数据绑定到文本框的示例。这是我的风格:

<Style x:Key="ValidationTextBox" TargetType="{x:Type Control}"> 
      <Setter Property="VerticalAlignment" Value="Center"/> 
      <Setter Property="Margin" Value="0,2,40,2"/> 
      <Setter Property="Validation.ErrorTemplate"> 
       <Setter.Value> 
        <ControlTemplate> 
         <DockPanel LastChildFill="True"> 
          <Border Background="#B22222" DockPanel.Dock="Right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10" 
            ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"> 
           <TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White"/> 
          </Border> 
          <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center"> 
           <Border BorderBrush="#B22222" BorderThickness="1" /> 
          </AdornedElementPlaceholder> 
         </DockPanel> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style 

<TextBox Style="{StaticResource ValidationTextBox}" Text="{Binding Path=Customer.LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />

+0

不幸的是,这只显示感叹号上的工具提示,我'd喜欢为整个文本框显示工具提示(或任何控制出错) – Justin 2013-02-28 09:53:44

0

正如我在my answer here说,您可以:

<ControlTemplate x:Key="ErrorTemplate"> 
    <Border BorderThickness="1" BorderBrush="Red" 
      Background="Transparent" 
      ToolTip="{Binding Path=/ErrorContent}"> 
     <AdornedElementPlaceholder /> 
    </Border> 
</ControlTemplate> 
+0

这有助于显示工具提示,但也可以防止与底层控件的任何交互。我可以使用IsHitTestVisible =“False”修复这个问题,但不幸的是,我现在回到了与之前完全相同的状态 - 没有任何工具提示! – Justin 2013-02-28 09:46:56

0

对不起,我昨天没有时间......你会试试下面,看看这是你以后的样子吗?

<Style x:Key="ValidationTextBox2" TargetType="{x:Type Control}"> 
      <Setter Property="VerticalAlignment" Value="Center"/> 
      <Setter Property="Validation.ErrorTemplate"> 
       <Setter.Value> 
        <ControlTemplate> 
         <Border BorderBrush="Red" BorderThickness="2"> 
          <DockPanel LastChildFill="True" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" Background="Transparent"> 
           <TextBlock /> 
          <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center"> 
          </AdornedElementPlaceholder> 
         </DockPanel> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
相关问题