Sunday 29 April 2012

Validation In Silverlight


By default silverlight doesn't give any visual feedback when the data binding system encounters error relating to incorrect input. But internally it rejects data and user is thinking that data has been saved. Silverlight data binding engine has to notify or alert us through visual appearance when there is a violation of rule.

Lets begin :)

1. Create a silverlight application and add the following Student class to your project

public class Student
{
public string Name{get; set;}
public int Age{get; set;}
}

2.//MainPage.xaml.cs

public MainPage()
{
initializeComponent();
Student std= new Student();
LayoutRoot.DataContext =std;
}

3.//MainPage.xaml
Run the application and type string in the age field . Internal validation has happened by rejecting the  invalid input but no visual feedback.

4.ValidatesOnExceptions = True, by doing that, it will makes sure silverlight will provide visual feedback for the validation error.
<TextBox Name="txtName" Text="{Binding Name,ValidatesOnExceptions=True}"
<TextBox Name="txtAge" Text="{Binding Age,ValidatesOnExceptions=True}"

5. Again type string value in Age textbox, this appearance will come :)




No comments:

Post a Comment