Click here to Skip to main content
Click here to Skip to main content
Technical Blog

So what's the difference between a Custom Control and User Control?

, 9 Apr 2011 CPOL
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 should one 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 to you 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 a 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 a 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 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.

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 _
Technical Lead
India India
Kunal Chowdhury is a Microsoft "Client Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, 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 8 (WinRT). He posts his findings, articles in his technical blog and CodeProject.
 
Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalSibeesh Venu1-Aug-14 5:29 
GeneralMy vote of 5 Pinmemberpolczym3-Aug-13 0:45 
GeneralMy vote of 5 PinmemberMember 969775425-Jun-13 4:23 
QuestionUserControl can't be Dragged-n-Dropped from toolbox? Pinmembersupernorb30-Apr-13 17:18 
Questionlike it Pinmemberdev C plus22-Apr-13 18:11 
GeneralMy vote of 5 PinmemberShreyash Gajbhiye18-Apr-13 20:11 
GeneralMy vote of 5 Pinmemberhiiizz14-Feb-13 5:31 
GeneralMy vote of 5 PinmemberDatta Salunkhe1-Jan-13 1:12 
GeneralMy Vote of 5 PinmemberPankaj Babre8-Dec-12 4:56 
GeneralMy vote of 4 PinmemberTamil Selvan K16-Jul-12 19:06 
GeneralMy vote of 5 Pinmembernikhil _singh16-Jul-12 2:36 
GeneralMy vote of 5 PinmemberP.Salini29-Jun-12 0:07 
GeneralMy vote of 4 PinmemberMuthukumar Nadar18-May-12 17:13 
GeneralMy vote of 5 Pinmemberkushagrajo30-Apr-12 9:58 
GeneralMy vote of 5 Pinmemberh3220-Apr-11 23:19 
Never even thought about that!
GeneralMy vote of 5 PinmvpAbhijit Jana12-Apr-11 2:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 10 Apr 2011
Article Copyright 2011 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid