Posted by: Zeeshan Amjad | January 16, 2010

Data validation rules in Binding


We can define data validation by using the data validation class. This class is inherited by Object and we have to overload only one method “Validate” in it. Here is a class diagram of validation class.

ValidationRule

Here is out simple implementation of validation rule.

  1: public class MyValidation : ValidationRule
  2: {
  3:     public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
  4:     {
  5:         int number = Int32.Parse((String)value);
  6: 
  7:         if (number < 0 || number > 100)
  8:         {
  9:             return new ValidationResult(false, "Percentage should be in between 0 and 100");
 10:         }
 11:         return new ValidationResult(true, null);
 12:     }
 13: }
 14: 

Now lets revisited our range base program. We bind slider with scroll bar, progress bar and text box. But if we entered value greater than 100 then we will get the error validation. Here is complete XAML code of our program.

  1: <Window x:Class="WpfValidationRule.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:local="clr-namespace:WpfValidationRule"
  5:     Title="Valiation Rule" Height="300" Width="300">
  6:     <Grid>
  7:         <Grid.ColumnDefinitions>
  8:             <ColumnDefinition/>
  9:             <ColumnDefinition/>
 10:         </Grid.ColumnDefinitions>
 11:         <Grid.RowDefinitions>
 12:             <RowDefinition/>
 13:             <RowDefinition/>
 14:             <RowDefinition/>
 15:             <RowDefinition/>
 16:         </Grid.RowDefinitions>
 17:         <TextBlock Grid.Column="0" Grid.Row="0" VerticalAlignment="Center"
 18:                    Margin="5" Text="Enter Percentage"/>
 19:         <TextBox Grid.Column="1" Grid.Row="0" VerticalAlignment="Center"
 20:                  Margin="5" Name="txtValue">
 21:             <TextBox.Text>
 22:                 <Binding Path="Value" ElementName="slider">
 23:                     <Binding.ValidationRules>
 24:                         <local:MyValidation/>
 25:                     </Binding.ValidationRules>
 26:                 </Binding>
 27:             </TextBox.Text>
 28:         </TextBox>
 29:         <ScrollBar Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Margin="5" 
 30:                    Orientation="Horizontal" Minimum="0" Maximum="110"
 31:                    Value="{Binding ElementName=slider, Path=Value}"/>
 32:         <ProgressBar Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Margin="5" 
 33:                    Orientation="Horizontal" Minimum="0" Maximum="110"
 34:                      Value="{Binding ElementName=slider, Path=Value}"/>
 35:         <Slider Name="slider" Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Margin="5" 
 36:                    Orientation="Horizontal" Minimum="0" Maximum="110"/>
 37:     </Grid>
 38: </Window>
 39: 

And here is complete C# code of our program.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Linq;
  4: using System.Text;
  5: using System.Windows;
  6: using System.Windows.Controls;
  7: using System.Windows.Data;
  8: using System.Windows.Documents;
  9: using System.Windows.Input;
 10: using System.Windows.Media;
 11: using System.Windows.Media.Imaging;
 12: using System.Windows.Navigation;
 13: using System.Windows.Shapes;
 14: 
 15: namespace WpfValidationRule
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         public Window1()
 23:         {
 24:             InitializeComponent();
 25:         }
 26:     }
 27: 
 28:     public class MyValidation : ValidationRule
 29:     {
 30:         public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
 31:         {
 32:             int number = Int32.Parse((String)value);
 33: 
 34:             if (number < 0 || number > 100)
 35:             {
 36:                 return new ValidationResult(false, "Percentage should be in between 0 and 100");
 37:             }
 38:             return new ValidationResult(true, null);
 39:         }
 40:     }
 41: }
 42: 

This would be the output of our program if we enter any value in between 0 and 100 in the text box.

ValidationRuleOutput_01

But if we input any value greater than 100 then our validation rule will fail and this would be our output.

ValidationRuleOutput_02

Advertisements

Responses

  1. […] already saw one example of validation error here. Now we are going to make the same program but display the progress bar inside the status bar. We […]

  2. This way is good to validate presentation level data. But what if you need validate business level or dal level data. Once I read article http://vitana-group.com/article/microsoft-.net/validation and now use it in my projects. It is really flexible and because allow to validate any entity property.

  3. […] We already saw one method of data validation using ValidationRule class here. Now we are going to see validation using another technique, i.e. using IDataErrorInfo interface. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: