That's a good question that came up after the series in my blog. Someone asked why one should use a Custom Control instead of a User Control? Hence, let's extend the series and discuss about that. I apologize for the delay of this post. I should have explained this at the start of the series, but actually my goal was to explain how to create and use a 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 a search engine result, I will recommend you read my series of articles on Custom Controls after reading this post. That will help you to understand how to create a Custom Control in Silverlight. You can find the chapters here:
- How to create a Custom Control in Silverlight
- How to design a Custom Control by editing the Part Template
- How to implement Template Binding in a Silverlight Custom Control
- How to access Control Template parts from Code Behind
Let's start by describing the differences between a Custom Control and a User Control.
What is a Custom Control?
A custom control is a loosely coupled control defined in a class, which derives from Control
. The UI of the custom control is generally defined in a Resource Dictionary inside the resource file. We can create themes for a custom control and reuse it in various projects very easily.
Button
, CheckBox
, TextBox
etc., even a UserControl
is nothing but a Custom Control. You can easily load them inside an XAML page.
A Custom Control generally inherits from the System.Windows.Controls.Control
class. You may derive from a different custom control 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 the code, thus gives you more flexibility to extend the behaviour. Once you build and add a reference of the 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 it very easily.
What is a User Control?
The base "UserControl
" is nothing but a Custom Control that you derive to create a control UI specific to your project. Generally, we create a UserControl
which is placed inside an XAML page with tight bonding to the code behind. You can directly access its UI elements from the code-behind and do some specific operations.
A custom UserControl
inherits from the System.Windows.Controls.UserControls
class, which inherits from the base "Control
" class.
You can't create theming support for UserControl
s but can style them by creating themes for the child Custom Controls because they represent a collection of controls. Once you create a UserControl
UI in one project, you can't change it in other projects.
Difference Between a CustomControl and a UserControl
So now you have got the difference between a Custom Control and a User Control, I guess. Let's summarize the differences again. Read the comparison below to make things clear:
Custom Control | User Control |
A loosely 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 a ResourceDictionary | Defines UI as normal XAML |
UI is skinable | Child controls are skinable |
Has dynamic layout | Has static layout |
UI can be changed in different projects | UI is fixed and can't have different looks in different project |
Has full toolbox support | Can't be added to the toolbox |
Defines a single control | Defines a set of controls |
More flexible | Not very flexible like a Custom Control |
Requires in-depth knowledge of Silverlight UI Model | Does not require indepth knowledge of the UI Model |
Hope you now have a better understanding from the above comparison table.
When to Use?
Good question: "When to use a Custom Control?" Haven't you got the inner meaning of it yet? OK, read the summarized points below:
- 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
. - When you want to use your control in different projects and each project may want to change the look, use
CustomControl
. - When you want to implement some additional functionality for a control, create a
CustomControl
derived from the base control. - When you want to apply themes to your controls, 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, use
CustomControl
.
End Note
Hope this information will help you to understand the differences between Custom Controls and User Controls. Also, it will help you understand the use of each. Now you will be able to decide where to use which.
I appreciate your feedback and suggestions. Share this information with others. Follow me on Twitter @kunal2383. Also follow my other Silverlight related news blog: Silverlight-Zone.