带工具提示的WPF自定义验证器 [英] WPF custom validator with tooltip
本文介绍了带工具提示的WPF自定义验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为我的WPF应用创建自定义验证器模板.
I'd like to create a custom validator template for my WPF app.
我有一个工具提示模板:
I have a tooltip template:
<ControlTemplate x:Key="ToolTipTemplate" TargetType="ToolTip">
<Grid Opacity="0.93" MaxWidth="200">
<Border BorderBrush="Black" BorderThickness="1,1,1,1" CornerRadius="2">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFF5FAFE" Offset="0"/>
<GradientStop Color="#FFE5EFF9" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Border.Effect>
<DropShadowEffect Color="#FF393939" />
</Border.Effect>
<TextBlock Margin="10,10,10,10"
VerticalAlignment="Top"
TextWrapping="Wrap"
HorizontalAlignment="Left"
Text="{TemplateBinding Content}">
</TextBlock>
</Border>
</Grid>
</ControlTemplate>
另一个验证模板:
<ControlTemplate x:Key="GlobalErrorTemplate">
<DockPanel>
<Ellipse x:Name="PART_icon"
DockPanel.Dock="Right"
Margin="2,0"
Width="10" Height="10">
<Ellipse.Fill>
<LinearGradientBrush>
<GradientStop Color="#11FF1111" Offset="0" />
<GradientStop Color="#FFFF0000" Offset="1" />
</LinearGradientBrush>
</Ellipse.Fill>
<Ellipse.ToolTip>
<ToolTip Template="{StaticResource ToolTipTemplate}" />
</Ellipse.ToolTip>
</Ellipse>
<AdornedElementPlaceholder x:Name="adorner" />
</DockPanel>
</ControlTemplate>
以及控件的样式,例如:
And the styles for controls, eg:
<Style TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource GlobalErrorTemplate}"/>
<!--<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>-->
</Style>
如何将椭圆的工具提示设置为(Validation.Errors)[0] .ErrorContent ?
How can I set the tooltip of the Ellipse to (Validation.Errors)[0].ErrorContent?
谢谢, 玛特
推荐答案
ErrorTemplate的DataContext已经是Validation.Errors的值,因此您可以将工具提示定义为:
The DataContext of the ErrorTemplate is already the value of Validation.Errors, so you can just define your tooltip as:
<ToolTip Template="{StaticResource ToolTipTemplate}" Content="{Binding Path=[0].ErrorContent}" />
这篇关于带工具提示的WPF自定义验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文