Click here to Skip to main content
13,295,409 members (63,137 online)
Click here to Skip to main content
Add your own
alternative version

Stats

149.4K views
37 bookmarked
Posted 10 Apr 2011

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

, 10 Apr 2011
Rate this:
Please Sign up or sign in to vote.
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.

License

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

Share

About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer 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 Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Book authored:

Connect with me on:

My CodeProject Awards/Prizes:


You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionWhat is the difference between a Control Template and Custom Control ? Pin
Ammar Shaukat18-Sep-17 21:22
memberAmmar Shaukat18-Sep-17 21:22 
GeneralMy vote of 5 Pin
Sibeesh Venu1-Aug-14 6:29
professionalSibeesh Venu1-Aug-14 6:29 
GeneralMy vote of 5 Pin
polczym3-Aug-13 1:45
memberpolczym3-Aug-13 1:45 
GeneralMy vote of 5 Pin
Member 969775425-Jun-13 5:23
memberMember 969775425-Jun-13 5:23 
QuestionUserControl can't be Dragged-n-Dropped from toolbox? Pin
supernorb30-Apr-13 18:18
membersupernorb30-Apr-13 18:18 
Questionlike it Pin
dev C plus22-Apr-13 19:11
memberdev C plus22-Apr-13 19:11 
GeneralMy vote of 5 Pin
Shreyash Gajbhiye18-Apr-13 21:11
memberShreyash Gajbhiye18-Apr-13 21:11 
GeneralMy vote of 5 Pin
hiiizz14-Feb-13 6:31
memberhiiizz14-Feb-13 6:31 
GeneralMy vote of 5 Pin
Datta Salunkhe1-Jan-13 2:12
memberDatta Salunkhe1-Jan-13 2:12 
GeneralMy Vote of 5 Pin
Pankaj Babre8-Dec-12 5:56
memberPankaj Babre8-Dec-12 5:56 
GeneralMy vote of 4 Pin
Tamil Selvan K16-Jul-12 20:06
memberTamil Selvan K16-Jul-12 20:06 
GeneralMy vote of 5 Pin
nikhil _singh16-Jul-12 3:36
membernikhil _singh16-Jul-12 3:36 
GeneralMy vote of 5 Pin
P.Salini29-Jun-12 1:07
memberP.Salini29-Jun-12 1:07 
GeneralMy vote of 4 Pin
Muthukumar Nadar18-May-12 18:13
memberMuthukumar Nadar18-May-12 18:13 
GeneralMy vote of 5 Pin
kushagrajo30-Apr-12 10:58
memberkushagrajo30-Apr-12 10:58 
GeneralMy vote of 5 Pin
h3221-Apr-11 0:19
memberh3221-Apr-11 0:19 
Never even thought about that!
GeneralMy vote of 5 Pin
Abhijit Jana12-Apr-11 3:04
mvpAbhijit Jana12-Apr-11 3: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 10 Apr 2011
Article Copyright 2011 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid