Click here to Skip to main content
Click here to Skip to main content

Silverlight user control validation

By , 3 Dec 2011
 

Introduction

This article discusses user control validation techniques in Silverlight. It will help you display validation errors in user controls as they get displayed in textbox, combobox controls.

Problem definition

Very often in programming we require to create our own user controls which encapsulate system controls like TextBox, ComboBox, etc. Silverlight, with the use of INotifyDataErrorInfo, allows binding a source property to throw validation errors which become trickier to display in user controls.

Attempt for solution

One of the simplest solutions to overcome this problem is to bind internal system controls of user controls to ViewModel properties. But it makes the user control tightly coupled to a particular ViewModel, adding unnecessary restrictions in control usage. Though there is nothing innocuous in doing this (User Control aware of ViewModel and its properties), we definitely prefer to have a control independent of the ViewModel.

We can do this by making:

  • User Control expose properties shielding the internal controls in the User Control.
  • These properties are used as binding source to internal controls.

Having achieved View Model independence, we still have the control without validation notifications. As the ViewModel properties get bound with these kinds of proxy properties of user controls, none of the validation errors get displayed in the screen (you can refer to DumbControl.xaml and DumbControl.xaml.cs).

Let's try to visualize the above futile attempt:

It is clear that internal controls do not get error messages thrown by the View Model even though they have all binding settings.

This is because for these internal controls, binding source properties are user control properties, and the user control does not implement INotifyDataErrorInfo to validate these properties.

Even if it had implemented INotifyDataErrorInfo, it would have been impossible to write the View Model validations. Here are some solution(s):

Highlighting the internal control

What if we try to make the internal control somehow bind to the actual binding source (that is View Model properties)? Yeah, this would make the difference, provided we have a way to achieve this.

public static class FrameworkElementExtension
{
  public static void MapBinding(this FrameworkElement element, 
         DependencyProperty firstProperty, FrameworkElement targetElement, 
         DependencyProperty secondProperty)
  {
     BindingExpression firstExpression = element.GetBindingExpression(firstProperty);
     if (firstExpression != null && firstExpression.ParentBinding != null)
     {
      targetElement.SetBinding(secondProperty, firstExpression.ParentBinding);
     }
   }
}

Here I have created an extension method on the FrameworkElement allowing to clone the binding done on one property to some other control's dependency property. Thus the internal controls get bound to actual View Model properties, and subsequently inheriting the validation notifications.

Highlighting the entire user control

It is OK to highlight individual controls inside a user control, but sometimes we may need to display validation across the entire control.

VisualState.png

Here I have used the VisualStateManager to handle various visual states of the control. As the validation error is a sub-state of the control, we can highlight the validation error by controlling the toolkit and border appearance.

Conclusion

You can use any one of the above approaches depending on your need to handle validations for Silverlight user controls. The attached source code demonstrates the approaches mentioned in the article. Hope you will find it useful.

UserControlValidations.png

References

License

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

About the Author

RakeshGunijan
Software Developer
India India
Member
Hi,
I am Bachelor of Engg(Computer) from Mumbai University.
I am right now working in Singapore.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberChristian Amado1 Aug '12 - 11:03 
QuestionWarning with Validator in SilverlightmemberPrithvi Chauhan27 Apr '12 - 1:37 
GeneralMy vote of 5memberMember 84130373 Apr '12 - 4:47 
QuestionExcellent!memberSunasara Imdadhusen1 Mar '12 - 23:40 
Questionvote 5memberThatsAlok23 Feb '12 - 2:37 
GeneralMy vote of 4memberAvinash64745 Feb '12 - 23:30 
QuestionVery useful!memberMR_SAM_PIPER7 Dec '11 - 13:37 
GeneralMy vote of 1 [modified]memberMember 80072796 Dec '11 - 20:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 4 Dec 2011
Article Copyright 2011 by RakeshGunijan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid