Click here to Skip to main content
Click here to Skip to main content
Go to top

Silverlight user control validation

, 3 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Silverlight uer control validation display techniques.

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)

Share

About the Author

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

Comments and Discussions

 
GeneralGood One! PinmemberBasab Dasgupta17-Mar-14 23:13 
GeneralMy vote of 4 PinmemberChristian Amado1-Aug-12 11:03 
QuestionWarning with Validator in Silverlight PinmemberPrithvi Chauhan27-Apr-12 1:37 
GeneralMy vote of 5 PinmemberMember 84130373-Apr-12 4:47 
QuestionExcellent! PinmemberSunasara Imdadhusen1-Mar-12 23:40 
Questionvote 5 PinmemberThatsAlok23-Feb-12 2:37 
GeneralMy vote of 4 PinmemberAvinash64745-Feb-12 23:30 
QuestionVery useful! PinmemberMR_SAM_PIPER7-Dec-11 13:37 
GeneralMy vote of 1 [modified] PinmemberMember 80072796-Dec-11 20:35 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 4 Dec 2011
Article Copyright 2011 by RakeshGunijan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid