In this blog I will show you how can we apply validation in WPF as displayed in below screenshot:

validation

Step 1: Design a keying screen like the image being displayed below,

Design 1

Step 2: Add a class User.cs,

  1. namespace DotnetBullet.WPFValidationWay1
  2. {
  3. public class User
  4. {
  5. #region Variables
  6. string _userName = string.Empty;
  7. int _age;
  8. string _mobileNo = string.Empty;
  9. #endregion
  10. #region Properties
  11. /// <summary>
  12. /// Gets or sets the name of the user.
  13. /// </summary>
  14. /// <value>
  15. /// The name of the user.
  16. /// </value>
  17. public string UserName
  18. {
  19. get
  20. {
  21. return _userName;
  22. }
  23. set
  24. {
  25. _userName = value;
  26. }
  27. }
  28. /// <summary>
  29. /// Gets or sets the age.
  30. /// </summary>
  31. /// <value>
  32. /// The age.
  33. /// </value>
  34. public int Age
  35. {
  36. get
  37. {
  38. return _age;
  39. }
  40. set
  41. {
  42. _age = value;
  43. }
  44. }
  45. /// <summary>
  46. /// Gets or sets the mobile no.
  47. /// </summary>
  48. /// <value>
  49. /// The mobile no.
  50. /// </value>
  51. public string MobileNo
  52. {
  53. get
  54. {
  55. return _mobileNo;
  56. }
  57. set
  58. {
  59. _mobileNo = value;
  60. }
  61. }
  62. #endregion
  63. }
  64. }
Step 3: Add a class RequiredFiedValidationRule.cs for Required Field Validation,
  1. using System.Globalization;
  2. using System.Windows.Controls;
  3. namespace DotnetBullet.WPFValidationWay1
  4. {
  5. class RequiredFiedValidationRule : ValidationRule
  6. {
  7. public RequiredFiedValidationRule()
  8. {
  9. }
  10. public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  11. {
  12. if (value.ToString().Length > 0)
  13. {
  14. return new ValidationResult(true, null);
  15. }
  16. else
  17. {
  18. return new ValidationResult(false, "Required Field Validation");
  19. }
  20. }
  21. }
  22. }
Step 4: Add Control Template like this:
  1. <ControlTemplate x:Key="validationTemplate">
  2. <DockPanel>
  3. <TextBlock Foreground="Red" FontSize="25" Text="*" DockPanel.Dock="Right">
  4. </TextBlock>
  5. <AdornedElementPlaceholder/>
  6. </DockPanel>
  7. </ControlTemplate>
Step 5: Add Style,
  1. <Style x:Key="InputControlErrors" TargetType="{x:Type TextBox}">
  2. <Style.Triggers>
  3. <Trigger Property="Validation.HasError" Value="true">
  4. <Setter Property="ToolTip"
  5. Value="{Binding RelativeSource={x:Static RelativeSource.Self},
  6. Path=(Validation.Errors)[0].ErrorContent}"/>
  7. </Trigger>
  8. </Style.Triggers>
  9. </Style>
Step 6: Modify textbox XAML,
  1. <TextBox HorizontalAlignment="Left" Height="23" Margin="159,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="258"
  2. Validation.ErrorTemplate="{StaticResource validationTemplate}" Style="{StaticResource InputControlErrors}"
  3. Name="txtName">
  4. <TextBox.Text>
  5. <Binding Path="UserName" Source="{StaticResource user}" UpdateSourceTrigger="PropertyChanged">
  6. <Binding.ValidationRules>
  7. <c:RequiredFiedValidationRule></c:RequiredFiedValidationRule>
  8. </Binding.ValidationRules>
  9. </Binding>
  10. </TextBox.Text>
  11. </TextBox>
Repeat the same for all the 3 controls.

The complete XAML code for UI is as follows:
  1. <Window x:Class="DotnetBullet.WPFValidationWay1.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:c="clr-namespace:DotnetBullet.WPFValidationWay1"
  5. Title="DotNetBullet.com Binding Validation Way" Height="250" Width="450">
  6. <Window.Resources>
  7. <c:User x:Key="user"/>
  8. <ControlTemplate x:Key="validationTemplate">
  9. <DockPanel>
  10. <TextBlock Foreground="Red" FontSize="25" Text="*" DockPanel.Dock="Right"></TextBlock>
  11. <AdornedElementPlaceholder/>
  12. </DockPanel>
  13. </ControlTemplate>
  14. <Style x:Key="InputControlErrors" TargetType="{x:Type TextBox}">
  15. <Style.Triggers>
  16. <Trigger Property="Validation.HasError" Value="true">
  17. <Setter Property="ToolTip"
  18. Value="{Binding RelativeSource={x:Static RelativeSource.Self},
  19. Path=(Validation.Errors)[0].ErrorContent}"/>
  20. </Trigger>
  21. </Style.Triggers>
  22. </Style>
  23. </Window.Resources>
  24. <Grid Margin="0,0,0,5">
  25. <Label Content="Name" HorizontalAlignment="Left" Margin="42,10,0,0" VerticalAlignment="Top"/>
  26. <Label Content="Age" HorizontalAlignment="Left" Margin="42,67,0,0" VerticalAlignment="Top"/>
  27. <Label Content="Mobile no." HorizontalAlignment="Left" Margin="42,127,0,0" VerticalAlignment="Top"/>
  28. <TextBox HorizontalAlignment="Left" Height="23" Margin="159,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="258"
  29. Validation.ErrorTemplate="{StaticResource validationTemplate}" Style="{StaticResource InputControlErrors}"
  30. Name="txtName">
  31. <TextBox.Text>
  32. <Binding Path="UserName" Source="{StaticResource user}" UpdateSourceTrigger="PropertyChanged">
  33. <Binding.ValidationRules>
  34. <c:RequiredFiedValidationRule></c:RequiredFiedValidationRule>
  35. </Binding.ValidationRules>
  36. </Binding>
  37. </TextBox.Text>
  38. </TextBox>
  39. <TextBox HorizontalAlignment="Left" Height="23" Margin="159,70,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="258"
  40. Validation.ErrorTemplate="{StaticResource validationTemplate}" Style="{StaticResource InputControlErrors}"
  41. Name="txtAge">
  42. <TextBox.Text>
  43. <Binding Path="Age" Source="{StaticResource user}" UpdateSourceTrigger="PropertyChanged">
  44. <Binding.ValidationRules>
  45. <c:RequiredFiedValidationRule></c:RequiredFiedValidationRule>
  46. </Binding.ValidationRules>
  47. </Binding>
  48. </TextBox.Text>
  49. </TextBox>
  50. <TextBox HorizontalAlignment="Left" Height="23" Margin="159,130,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="258"
  51. Validation.ErrorTemplate="{StaticResource validationTemplate}" Style="{StaticResource InputControlErrors}"
  52. Name="txtMobileNo">
  53. <TextBox.Text>
  54. <Binding Path="MobileNo" Source="{StaticResource user}" UpdateSourceTrigger="PropertyChanged">
  55. <Binding.ValidationRules>
  56. <c:RequiredFiedValidationRule></c:RequiredFiedValidationRule>
  57. </Binding.ValidationRules>
  58. </Binding>
  59. </TextBox.Text>
  60. </TextBox>
  61. </Grid>
  62. </Window>
If you face any issue regarding this code please feel free to ask me.