That's a good question came up after the series in my blog. Someone asked why should one use Custom Control instead of User Control? Hence, let's extend the series and discuss about that. I apologise the delay of this post. I should explain this at the start of the series, but actually my goal was to explain you how to create and use Custom Control in Silverlight.

 

It is better to do it later than never. Also, you are now familiar with Custom Control creation. Hence, start describing it. You will come to know more about this here. As always, don't forget to share your feedback at the end of this page.

 


Background

If you came here through Search Engine result, I will recommend you to read my series of articles on Custom Control after reading this post. Those will help you to understand how to create a Custom Control in Silverlight.You can find the chapters here:

  1. How to create a Custom Control in Silverlight?
  2. How to design a Custom Control by editing the Part Template?
  3. How to implement Template Binding in Silverlight Custom Control?
  4. How to access Control Template parts from Code Behind?

Let's start describing difference between Custom Control and User Control.

 

 

 

What is Custom Control?

A custom control is a loosely coupled control defined in a class, which derives from Control. The UI of custom control generally defined in a Resource Dictionary inside the resource file. We can create themes for custom control and reuse them in various projects very easily.

 

Button, CheckBox, TextBox etc. even a UserControl is nothing but a Custom Control. You can easily load them inside a XAML page.

 

image

 

A Custom control generally inherits from System.Windows.Controls.Control class. You may derive from a different custom control also depending on your requirement.

 

Custom Controls are compiled into a dll assembly and can be reused in multiple places very easily. You have total control over it's code, thus gives you more flexibility to extend the behaviour. Once you build and add reference of custom control in your project, you can find it in the toolbox. Thus, you will be able to drag and drop the control in your design view and start working with that very easily.

 

 

 

What is User Control?

The base "UserControl" is nothing but a Custom Control that you derive to create control UI specific to your project. Generally we create a UserControl which placed inside a XAML page with tightly bonding to it's code behind. You can directly access it's UI elements from code behind and do some specific operations.

 

image

 

A custom UserControl inherits from System.Windows.Controls.UserControls class, which inherits from base "Control" class.

 

You can't create theming support for UserControls but can style them by creating theme for it's child Custom Controls because they represents a collection of controls. Once you create a UserControl UI in once project, you can't change them in the other projects.

 

 

 

Difference between CustomControl and UserControl

So, now you got the difference between Custom Control and User Control, I guess. Let's summarize the difference again. Read the comparison below to make it clear:

 

Custom Control User Control
A loosly coupled control w.r.t code and UI A tightly coupled control w.r.t code and UI
Derives from Control Derives from UserControl
Defines UI in the ResourceDictionary Defines UI as a normal XAML
UI can be skinable Child controls can skinable only
Has Dynamic layout Has static layout
UI can be changed in different project UI is fixed and can't have different look in every project
Has full toolbox support Can't be added to the toolbox
Defines a single control Defines a set of controls
More flexible Not much flexible like Custom Control
Requires in depth knowledge of Silverlight UI Model Does not require depth knowledge of UI Model

 

Hope, you got a better understanding now from the above comparison table.

 

 

 

When to use?

Good question "When to use a Custom Control?" Didn't you get the inner meaning of it yet? Ok, read the below summarized points:

  • When you have a rapid and fixed content in your UI, use UserControl.
  • When you want to separate some basic functionality of your main view to some smaller pieces with reusability, use UserControl for that.
  • When you want to use your control in different projects and each project may want to change the look according to their need, use CustomControl.
  • When you want to implement some additional functionality of a Control, create a CustomControl derived from the base control.
  • When you want to apply theme to your control, use CustomControl.
  • When you want to add toolbox support for your control, so that, your user will be able to do drag and drop to the designer, in such case, use CustomControl.

 

 

End Note

Hope, this information will help you to understand the difference between Custom Control and User Control. Also, it will help you to understand the use of each. Now, you will be able to decide where to use which one.

 

Appreciate your feedback and suggestion. Share this information to others. Follow me in Twitter @kunal2383. Also, follow my another Silverlight related news Blog: Silverlight-Zone.


Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.