带工具提示的 WPF 自定义验证器
我想为我的 WPF 应用程序创建一个自定义验证器模板。
我有一个工具提示模板:
<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>
以及控件的样式,例如:
<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?
谢谢, 市场
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>
Another template for validation:
<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>
How can I set the tooltip of the Ellipse to (Validation.Errors)[0].ErrorContent?
Thanks,
Mart
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ErrorTemplate 的 DataContext 已经是 Validation.Errors 的值,因此您可以将工具提示定义为:
The DataContext of the ErrorTemplate is already the value of Validation.Errors, so you can just define your tooltip as: