我有一个绑定(bind)到某些属性的TextBox。我已经实现了IDataErrorInfo来执行验证。最近,我在网上看到一些控件,该控件显示了一个类似红色三角形的错误。
我已附上以下示例:
我知道我必须编写错误模板才能在发生错误时显示此消息。当用户将鼠标悬停在红色三角形上时,它将在工具提示中显示错误消息。我如何显示错误的文本框,就像我上传的文本框一样。如何在错误模板中获得红色三角形?
最佳答案
这是一个看起来像这样的例子
像这样使用
<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 - 在WPF中,如何在TextBox中显示验证错误,如下图所示?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7147894/