C# 有没有办法在ErrorTemplate中设置工具提示?
在我们的WPF应用程序中,我们有一个通用的控件模板,用于以一致的方式显示错误C# 有没有办法在ErrorTemplate中设置工具提示?,c#,wpf,errortemplate,C#,Wpf,Errortemplate,在我们的WPF应用程序中,我们有一个通用的控件模板,用于以一致的方式显示错误 <ResourceDictionary> <ControlTemplate x:Key="ErrorTemplate"> <Border BorderThickness="1" BorderBrush="Red"> <AdornedElementPlaceholder /> </Border>
<ResourceDictionary>
<ControlTemplate x:Key="ErrorTemplate">
<Border BorderThickness="1" BorderBrush="Red">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
</ResourceDictionary>
在应用程序的其他地方,当控件可能显示错误时,我们将ErrorTemplate设置为
<TextBox Validation.ErrorTemplate="{DynamicResource ErrorTemplate}" />
现在我想在此错误模板中显示工具提示,但是在边框上设置tooltip属性没有多大帮助,因为工具提示仅在用户将鼠标移到1px宽的边框上时显示,而不是在出错的控件本身上显示
我知道我可以在样式中设置工具提示,但是此错误模板应用于许多不同的控件(组合框等),并且这些控件中的许多控件也使用独立于我的错误模板的样式-我真的希望能够以通用方式将我的错误模板应用于任何控件
有什么方法可以在ErrorTemplate中设置工具提示吗?我定义了一个样式。我的对象(Customer)上有IDataErrorInfo,它对数据绑定到文本框的属性(LastName)进行验证。这是我的风格:
正如我在中所述,您可以:
很抱歉,我昨天没有时间……请尝试下面的内容,看看这是否是您想要的,好吗
这有助于显示工具提示,但也可以防止与基础控件的任何交互。我可以用ishitestvisible=“False”
来解决这个问题,但不幸的是,我又回到了与以前完全相同的情况——没有工具提示!不幸的是,这只显示感叹号上的工具提示,我希望为整个文本框(或任何控件出错的控件)显示工具提示,非常确定这无法完成-下面介绍了我见过的以通用方式应用样式来完成此操作的最佳方法
<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
<ControlTemplate x:Key="ErrorTemplate">
<Border BorderThickness="1" BorderBrush="Red"
Background="Transparent"
ToolTip="{Binding Path=/ErrorContent}">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
<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>