In WPF, How to display validation error in TextBox like the image below?
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 err开发者_如何学Pythonor like a red triangle. I have attached the sample below:
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
Use it like
<TextBox Validation.ErrorTemplate="{StaticResource topRightCornerErrorTemplate}"
.../>
ErrorTemplate
<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>
精彩评论