在 WPF 中,如何在 TextBox 中显示验证错误,如下图所示? [英] In WPF, How to display validation error in TextBox like the image below?
问题描述
我有一个绑定到某个属性的 TextBox.我已经实现了 IDataErrorInfo 来执行验证.最近我在 web 中看到一些控件,它显示了一个红色三角形之类的错误.我附上了下面的样本:
I have a TextBox bound to some property. I have implemented IDataErrorInfo for performing validation. Recently I was seeing some control in web which shows an error like a red triangle. I have attached the sample below:
我知道我必须编写错误模板以在发生错误时显示此内容.当用户将鼠标悬停在红色三角形上时,它会在 ToolTip 中显示错误消息.如何显示与我上传的错误文本框类似的错误文本框.如何获取错误模板中的红色三角形?
I know I have to write error template to display this whenever an error occurs. When the user hovers the red triangle, it will display the error message in ToolTip. How do I display an error textBox like the one I have uploaded. How to get the red triangle in error template?
推荐答案
这是一个看起来像这样的例子
Here is an example that looks like this
像这样使用
<TextBox Validation.ErrorTemplate="{StaticResource topRightCornerErrorTemplate}"
.../>
错误模板
<ControlTemplate x:Key="topRightCornerErrorTemplate">
<Grid>
<Polygon Points="40,20 40,0 0,0"
Stroke="Black"
StrokeThickness="1"
Fill="Red"
HorizontalAlignment="Right"
VerticalAlignment="Top"
ToolTip="{Binding ElementName=adorner,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
<AdornedElementPlaceholder x:Name="adorner"/>
</Grid>
</ControlTemplate>
这篇关于在 WPF 中,如何在 TextBox 中显示验证错误,如下图所示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!