Wednesday, December 9, 2009

Silverlight 3 : Validation

To do validation in silverlight 3 , need to use dataannotation. In my previous post i discussed about binding and resource file.

1. Sample XAML File.

<UserControl xmlns:dataInput="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.Input"  xmlns:dataFormToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"  x:Class="SilverlightApplication2.MainPage"    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:SilverlightApplication2.Resourses" xmlns:localCustom="clr-namespace:SilverlightApplication2"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
    <UserControl.Resources>
        <local:Strings x:Key="LocStrings"></local:Strings>
        <localCustom:DisplayLabelConvertor x:Key="display"></localCustom:DisplayLabelConvertor>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="#FF333333">     
        <StackPanel Orientation="Vertical" x:Name="MyDataForm" BindingValidationError="MyDataForm_BindingValidationError" >
            <dataInput:ValidationSummary x:Name="valida" Height="100"></dataInput:ValidationSummary>     

            <TextBox x:Name="myName" Text="{Binding Name,Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=True}" >
            </TextBox>         
                <Button Content="save" Click="Button_Click"></Button>
        </StackPanel>
    </Grid>
</UserControl>

2. CS File.

public partial class MainPage : UserControl
    {
        Customer c = new Customer();
        private int _errorCount = 0;
        public MainPage()
        {
            InitializeComponent();       
            MyDataForm.DataContext = c;
        }

        private void MyDataForm_BindingValidationError(object sender, ValidationErrorEventArgs e)
        {
            if (e.Action == ValidationErrorEventAction.Added)
                _errorCount++;
            else if (e.Action == ValidationErrorEventAction.Removed)
                _errorCount--;
        }     

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _errorCount = 0;

            // Need to do for validating control.
            // If you do not do this then untill you make any change in control no validation will work.
            // Like form first time open and you prees submit then it will not validate . To do so this is required.
            BindingExpression exp = myName.GetBindingExpression(TextBox.TextProperty);
            if (exp != null)
            exp.UpdateSource();

            if (_errorCount ==0)
            {
                //Success
            }
            else
            {
                //Error
            }
        }

    }

3. Enjoy Validation………… :)

Let me know your comment.

No comments: