Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 error like a red triangle. I have attached the sample below:

enter image description here

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?

like image 522
Gurucharan Balakuntla Maheshku Avatar asked Aug 22 '11 13:08

Gurucharan Balakuntla Maheshku


1 Answers

Here is an example that looks like this

enter image description here

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>
like image 134
Fredrik Hedblad Avatar answered Sep 22 '22 11:09

Fredrik Hedblad