在我们的WPF应用程序中,我们有一个通用的控件模板,用来以一致的方式显示错误
<ResourceDictionary>
<ControlTemplate x:Key="ErrorTemplate">
<Border BorderThickness="1" BorderBrush="Red">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
</ResourceDictionary>在我们的应用程序的其他地方,当控件可能显示错误时,我们像这样设置ErrorTemplate
<TextBox Validation.ErrorTemplate="{DynamicResource ErrorTemplate}" />我现在想在这个错误模板中显示一个工具提示,但是在边框上设置tooltip属性没有太大帮助,因为工具提示只在用户将鼠标移到1px宽的边框上时显示,而不是错误的控件本身。
我知道我可以在样式中设置工具提示,但是这个错误模板适用于许多不同的控件(组合框等)。其中许多控件也使用独立于我的错误模板的样式-我真的希望能够以通用的方式将我的错误模板应用于任何控件。
有什么方法可以在我的ErrorTemplate中设置工具提示吗?
发布于 2013-02-28 01:59:16
我有一个定义的风格。我的对象(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}" />
发布于 2013-02-28 02:42:52
正如我在my answer here中所说的,您可以:
<ControlTemplate x:Key="ErrorTemplate">
<Border BorderThickness="1" BorderBrush="Red"
Background="Transparent"
ToolTip="{Binding Path=/ErrorContent}">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>发布于 2013-03-01 21:49:22
我很抱歉我没有时间yesterday...Would你试试下面看看这是不是你想要的,好吗?
<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>https://stackoverflow.com/questions/15118706
复制相似问题