Click here to Skip to main content
15,663,033 members
Articles / Web Development / HTML
Posted 1 Dec 2011


10 bookmarked

Silverlight User Control Validation

Rate me:
Please Sign up or sign in to vote.
4.42/5 (9 votes)
3 Dec 2011CPOL2 min read
Silverlight user control validation display techniques


This tip 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:

Image 1

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.


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.


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 tip. Hope you will find it useful.



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

Written By
Software Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralGood One! Pin
Basab Dasgupta17-Mar-14 23:13
Basab Dasgupta17-Mar-14 23:13 
GeneralMy vote of 4 Pin
Christian Amado1-Aug-12 11:03
professionalChristian Amado1-Aug-12 11:03 
QuestionWarning with Validator in Silverlight Pin
Prithvi Chauhan27-Apr-12 1:37
Prithvi Chauhan27-Apr-12 1:37 
GeneralMy vote of 5 Pin
Member 84130373-Apr-12 4:47
Member 84130373-Apr-12 4:47 
QuestionExcellent! Pin
Sunasara Imdadhusen1-Mar-12 23:40
professionalSunasara Imdadhusen1-Mar-12 23:40 
Questionvote 5 Pin
ThatsAlok23-Feb-12 2:37
ThatsAlok23-Feb-12 2:37 
GeneralMy vote of 4 Pin
Avinash64745-Feb-12 23:30
Avinash64745-Feb-12 23:30 
QuestionVery useful! Pin
MR_SAM_PIPER7-Dec-11 13:37
MR_SAM_PIPER7-Dec-11 13:37 
GeneralMy vote of 1 Pin
Arterius6-Dec-11 20:35
Arterius6-Dec-11 20:35 

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

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