Click here to Skip to main content
15,880,651 members
Articles / Web Development / ASP.NET

So What's the Difference Between a Custom Control and User Control?

Rate me:
Please Sign up or sign in to vote.
4.90/5 (46 votes)
9 Apr 2011CPOL4 min read 198.9K   38   17
That's a good question that came up after the series in my blog.

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:

  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 a Silverlight Custom Control
  4. 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.

image

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.

image

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 UserControls 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 ControlUser Control
A loosely coupled control w.r.t code and UIA tightly coupled control w.r.t code and UI
Derives from ControlDerives from UserControl
Defines UI in a ResourceDictionaryDefines UI as normal XAML
UI is skinableChild controls are skinable
Has dynamic layoutHas static layout
UI can be changed in different projectsUI is fixed and can't have different looks in different project
Has full toolbox supportCan't be added to the toolbox
Defines a single controlDefines a set of controls
More flexibleNot very flexible like a Custom Control
Requires in-depth knowledge of Silverlight UI ModelDoes 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.

This article was originally posted at http://www.kunal-chowdhury.com/feeds/posts/default

License

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


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
QuestionWhat is the difference between a Control Template and Custom Control ? Pin
Ammar Shaukat18-Sep-17 20:22
professionalAmmar Shaukat18-Sep-17 20:22 
GeneralMy vote of 5 Pin
Sibeesh Venu1-Aug-14 5:29
professionalSibeesh Venu1-Aug-14 5:29 
GeneralMy vote of 5 Pin
polczym3-Aug-13 0:45
polczym3-Aug-13 0:45 
GeneralMy vote of 5 Pin
YaPolo!25-Jun-13 4:23
professionalYaPolo!25-Jun-13 4:23 
QuestionUserControl can't be Dragged-n-Dropped from toolbox? Pin
supernorb30-Apr-13 17:18
supernorb30-Apr-13 17:18 
Questionlike it Pin
dev C plus22-Apr-13 18:11
dev C plus22-Apr-13 18:11 
GeneralMy vote of 5 Pin
Shreyash Gajbhiye18-Apr-13 20:11
professionalShreyash Gajbhiye18-Apr-13 20:11 
GeneralMy vote of 5 Pin
hiiizz14-Feb-13 5:31
hiiizz14-Feb-13 5:31 
GeneralMy vote of 5 Pin
Datta Salunkhe1-Jan-13 1:12
Datta Salunkhe1-Jan-13 1:12 
GeneralMy Vote of 5 Pin
Pankaj Babre8-Dec-12 4:56
Pankaj Babre8-Dec-12 4:56 
GeneralMy vote of 4 Pin
Tamil Selvan K16-Jul-12 19:06
Tamil Selvan K16-Jul-12 19:06 
GeneralMy vote of 5 Pin
Nikhil_S16-Jul-12 2:36
professionalNikhil_S16-Jul-12 2:36 
GeneralMy vote of 5 Pin
P.Salini29-Jun-12 0:07
P.Salini29-Jun-12 0:07 
GeneralMy vote of 4 Pin
Muthu Nadar18-May-12 17:13
Muthu Nadar18-May-12 17:13 
GeneralMy vote of 5 Pin
kushagrajo30-Apr-12 9:58
kushagrajo30-Apr-12 9:58 
GeneralMy vote of 5 Pin
h3220-Apr-11 23:19
h3220-Apr-11 23:19 
Never even thought about that!
GeneralMy vote of 5 Pin
Abhijit Jana12-Apr-11 2:04
professionalAbhijit Jana12-Apr-11 2:04 

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.