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.
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.
References