Click here to Skip to main content
15,885,720 members
Articles / Silverlight / Silverlight4

Validating Textbox on Lost Focus in RIA Service, Silverlight 4

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Apr 2011CPOL1 min read 8.1K  
How to validate Textbox on lost focus in RIA service, Silverlight 4

While working on my post on “Data Binding in Silverlight with RIA and Entity Framework – Part 3 (Validating Input Data)”, a series of articles on RIA Services, I came across a situation where I wanted to validate my textbox control on a particular event. For example, on LostFocous. As we know, the validation fires only when the source property value gets changed and the entity gets updated. But you might have known that the Textbox TextProperty gets updated on LostFocus (MSDN Link). This is the default behaviour but it does not work with RIA Service in Textbox.Baring teeth smile

And most of the time, the validation on the client side needs to be triggered explicitly on our demand. For example, refer to my StatesOfIndia application as the user provides some value at state Name and moves on,  the validation needs to be fired.

So to implement the validation on LostFocus of StateName textbox, follow the steps below.

Make Sure You Have Implemented the Validation Rule at Model Entity Member

In the Metadata file, I have added Required Attribute for the statename.

image

Change the UpdateSourceTrigger of Textbox

Then, change the UpdateSourceTrigger property of the binding of the Textbox to explicit mode.

image

Update the Source at your Desired Event

As I want to force updation of data as well as fire validation on lost focus event, I am going to get the binding expression of the control and update its source.

C#
private void txtStateName_LostFocus(object sender, RoutedEventArgs e) 
{ 
System.Windows.Data.BindingExpression bexpress = txtStateName.GetBindingExpression(
   TextBox.TextProperty); 
bexpress.UpdateSource(); 

}

As soon as the property gets changed, the validation will fire.

This is for now. Soon, I will post a detailed article on validation. Stay tuned! :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Microsoft
India India
Nothing special .. I like challenges and love my critics.

Microsoft | Bangalore | India

Blog : http://manaspatnaik.com/blog

Twitter@manas_patnaik

Comments and Discussions

 
-- There are no messages in this forum --