【发布时间】:2017-06-13 01:43:08
【问题描述】:
我正在开发 WPF 应用程序并希望实现验证。 为了显示错误消息等。我正在使用 TextBox 的样式:
<Style TargetType="{x:Type TextBox}">
<Setter Property="Height"
Value="25"/>
<Setter Property="VerticalAlignment"
Value="Top"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="true">
<Border Background="Red"
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">
</TextBlock>
</Border>
<AdornedElementPlaceholder Name="customAdorner"
VerticalAlignment="Center" >
<Border BorderBrush="Red"
BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<!--Additional style trigger for changing the background color of the textbox-->
<Style.Triggers>
<Trigger Property="Validation.HasError"
Value="true">
<Setter Property="Background"
Value="LightPink"/>
</Trigger>
</Style.Triggers>
</Style>
当我的一个文本框出现错误时,文本框会出现红色边框,右侧会出现一个带有白色“!”的红点。出现。 现在的问题是右边的红点与相邻元素重叠。 有没有办法避免这种事情?
您可以从这里下载一个示例: WPF validation example
然后选择项目“Validation_ValidationRule”作为启动项目。
提前致谢!
【问题讨论】: