- public void ValidateTextbox(ref TextBox ctrltxt, string p)
- {
- int count = 0;
- string errormessage=string.Empty;
- BindingExpression expression = ctrltxt.GetBindingExpression(TextBox.TextProperty);
- expression.UpdateSource();
- if (true) // check validation condition or reg expression here
- {
- ValidationError error = new ValidationError(new ExceptionValidationRule(), expression, errormessage, null);
- Validation.MarkInvalid(expression, error);
- }
- }
XAML code for the TEXTBOX is as below
- <TextBox Name="FieldName" HorizontalAlignment="Stretch"
- VerticalAlignment="Stretch"
- Text="{Binding Path=DisplayData, ValidatesOnDataErrors=True ,NotifyOnValidationError=True , UpdateSourceTrigger=LostFocus, ValidatesOnExceptions=True}"
- Grid.Column="1"
- Margin="4,4,4,4" Validation.ErrorTemplate="{StaticResource InputErrorTemplate}" >
- </TextBox>
Code
InputErrorTemplate is as belowHTML / XML
<ControlTemplate x:Key="InputErrorTemplate">
<DockPanel>
<Border Name="validationBorder" BorderBrush="Red" BorderThickness="2" Padding="1" CornerRadius="3">
<Border.Resources>
<Storyboard x:Key="_blink">
<ColorAnimationUsingKeyFrames AutoReverse="True" BeginTime="00:00:00" Storyboard.TargetName="validationBorder" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" RepeatBehavior="00:00:02">
<SplineColorKeyFrame KeyTime="00:00:00.5" Value="#00FF0000"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
</Border.Resources>
<Border.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource _blink}" />
</EventTrigger>
</Border.Triggers>
<AdornedElementPlaceholder/>
</Border>
</DockPanel>
</ControlTemplate>
Join the conversation! Your thoughts help the community grow.