Saturday, March 6, 2010

The pros and cons of UserControls

An anonymous commenter pointed out some problems with using a UserControl to build the Watermarked Textbox user control. I don't think the commenter read the whole post, but he points out some things that, perhaps, I should have discussed at the start of the first post. As I wrote in the very first post, there are two approaches to building the Watermarked TextBox control - a UserControl or a Templated Custom Control. Each approach has it's advantages and disadvantages.

It is worth while going over these advantages and disadvantages in more detail.

User Control

The advantages of using a User Control are mostly to do with being easier:
  1. The whole thing can be done easily in Blend, the small amount of code required can be written in the Blend IDE even though it is a poor cousin of Visual Studio for writing code.
  2. It is an easy to understand approach for beginners; no special attributes required and simple code.
  3. Quicker approach than creating a Templated User Control. For a one-off project that doesn't require building a flexible control that can be reused widely, a UserControl is certainly faster.
The disadvantages of using a User Control are generally to do with reuse and customizability. One of the wonderful things about Silverlight/WPF is the separation of presentation and functionality - for example a Button's functionality has very little to do with being a gray rectangle with a faux-3D border effect.

So the disadvantages, if you are after a reusable control, are:
  1. Harder to customize. Everything that you think you may want to style or localize needs to be re-exposed on the User Control. We only exposed the Text and Watermark properties, and a Style for the TextBox. This is not acceptable for a reusable control and the amount of work required to do so is such that we would be better to go down the Templated Custom Control path.
  2. Not very reusable. Following on from the first point, to use the same control in a second project will probably require changes to the UserControl itself; that's a pain.
For the purposed of my tutorial though, the UserControl was a useful approach to cover some basic techniques such as using states, dependency properties, and binding.

Templated Custom Control

A Templated Custom Control, when done well, is easy to use and easy to customize. From the both the point of view of a tutorial, and for use within a project though, there are some disadvantages. But first the advantages:
  1. The end result can be easily styled for each project that uses it since it has proper separation of functionality and presentation. This is a very important point and the only reason I used a UserControl in our project was because it was a one off that I could style how I needed it for that particular project.
  2. It is less effort if the end result needs to be fully localized - exposing every single property on a UserControl that may need localization is tedious.
  3. Validation is possible - we didn't need validation for our UserControl TextBox in the project I was working on because it was just a filtering text box to reduce the result set that was displayed in a list. But for use in a form requiring input validation, a Templated Custom Control is the correct approach.
And the disadvantages:
  1. Steeper Learning Curve. There are special requirements your code should meet if you are going to write a custom control; especially if you want it to play nicely with Blend. These are not enormously difficult, just one more thing that needs to be learned. If your needs are simple, and you are just learning Silverlight, then the extra learning curve can be daunting; especially considering the next point.
  2. The information available on creating custom controls is somewhat sparse. It is out there, but you may need to visit several sites to find everything you need. And you will want to look at quite a bit of source code from both the .Net framework (Reflector is your friend) and the excellent Silverlight Toolkit to get a good understanding of what is required.
So if you need a solution that is only going to be styled once, for a single project then there is nothing wrong in using a UserControl. Purists will still shake their head and mutter because things should be done the right way even for one-offs - but if you are working to a deadline then sometimes you need a simple and quick solution.

It's also a good learning exercise and a useful introduction into why a Templated Custom Control is a better approach. As stated from the beginning of the first post, I plan to come back to this topic in a later post and redo this control as a Templated Custom Control.


  1. Thanks for posting on this. I'm fairly new to SL and I've been a bit perplexed about the different roles that user controls and templated controls play. I'm glad to know that it's not just me having difficulty figuring out how templated controls work.

  2. I've finished coding up the Templated Custom Control now and will post the result and describe the technique as soon as I get the time.