Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  


The differences between CustomControls and UserControls

WPF has two concepts of controls: UserControls and CustomControls. But what's the difference between them? In this article I try to list the characteristics of each of them to help you to choose the right type for your project.

UserControl (Composition)

  • Composes multiple existing controls into a reusable "group"
  • Consists of a XAML and a code behind file
  • Cannot be styled/templated
  • Derives from UserControl

This example of an "RGB user control" composes three labels and textboxes as well as a color field together to an reusable part. The logic in the code behind file adds a Color DependencyProperty that gets and sets the resolved color.

CustomControl (Extending an existing control)

  • Extends an existing control with additional features
  • Consists of a code file and a default style in Themes/Generic.xaml
  • Can be styled/templated
  • The best approach to build a control library

This example of a "Numeric up/down" control is an extension of a textbox. The up and down buttons are defined in the default template of the control and wired up in the OnApplyTemplate() override in the logic part of the control. The ControlTemplate can easily be exchanged by another that has the up,down buttons aligned left for example.





Last modified: 2009-06-16 02:05:27
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
vy
Commented on 18.February 2009
Clear and helpful article. I love the examples
HH
Commented on 14.March 2009
Simple and clear
Alexandre...
Commented on 23.April 2009
Short but helpful!
Didn't know what to do, now i do ;)
Thanks
ss
Commented on 28.April 2009
good
MountBlade
Commented on 5.May 2009
nice
Sohel Shaik
Commented on 9.May 2009
Nice but needs to be explain more by example
Asif
Commented on 25.May 2009
I would suggest to put more example after brief explanation of the topic. But you have done an excellent job...
sreeram
Commented on 29.May 2009
thx for u r nice post but i am also supporting the suggestions of Sohel&asif put some more examples.....>
Prashant
Commented on 8.June 2009
some source code of the example here would have been great
Joseph Stanish
Commented on 28.June 2009
Nice, but too brief. A walkthrough with code would be more helpful.
Ashwanth
Commented on 16.July 2009
Nice and simple.
Chan
Commented on 12.August 2009
Very simple and good !
sumanth
Commented on 13.August 2009
hi i had created an user control now i want to addd that usercontrol top the xaml file can any body tell me
Vipul Vyas
Commented on 17.August 2009
christian Moser rocks man..
Matthew
Commented on 23.October 2009
It's good to attempt to simplify a subject like this, but I think this is far of the mark. There are plenty of reasons for creating a custom control that doesn't extend an existing control and derives directly from control. The most common reason is to support templating. In fact, your example--a custom color picker--is often done as a custom control for just that reason. There are plenty of WPF ColorPicker controls online that demonstrate this, including one in the MSDN at http://msdn.microsoft.com/en-us/library/ms771620.aspx.
Wazzzuup
Commented on 10.November 2009
So, what does someone mean when saying that usercontrols don't support styling and themes? I mean that if my UC contain button within and I put on form my UC and another button and then define style for TargetType="{x:Type Button}" so UC's button also would be styled!
Wazzzuup
Commented on 11.November 2009
So, what does someone mean when saying that usercontrols don't support styling and themes? I mean that if my UC contain button within and I put on form my UC and another button and then define style for TargetType="{x:Type Button}" so UC's button also would be styled!
Smith
Commented on 7.December 2009
Moser Can you please de-brief the above points.
Harendra kumar
Commented on 4.January 2010
Nice but needs to be explain more with few good examples.
ram
Commented on 5.January 2010
Nice Post
Shishir
Commented on 28.January 2010
Thank for the info!
chaitanya
Commented on 4.February 2010
Nice information, Thank u so much.
deep
Commented on 6.April 2010
wonderful
vijji
Commented on 28.April 2010
hi can u plz tell me how to add pdf image in wpf
suman
Commented on 5.May 2010
Nice Information
Michael
Commented on 6.May 2010
Can u please post a link to the code of these two controls?
Homero
Commented on 26.May 2010
What you say that the user controls cannot be styled?
They can be styled as any other control
Chennakesava...
Commented on 14.June 2010
Nice post and I too see that User controls can are stylable
Rajni Padhiyar
Commented on 12.July 2010
Hmmmmm.....

Thanks
Rajni Padhiyar
rajnicby.si@gmail.com
Adam
Commented on 28.July 2010
Good but a code walk-through of the Numeric Up/Down would be helpful.
dirty
Commented on 23.August 2010
COPYRIGHT INFRINGEMENT AT ITS FINEST!!!!!!!!!

MOSER IS GREASY AS HELL
Rahul Gupta
Commented on 2.October 2010
This is very Good But i think u have put more detail about the custom control.
msiravian
Commented on 13.October 2010
Good way of explanation. Simple and to the point.
msiravian
Commented on 15.October 2010
Good way of explanation. Simple and to the point.
serhio
Commented on 20.October 2010
cool. Brief and clear.
cp
Commented on 17.November 2010
Wow, it is that simple indeed!
parvendra
Commented on 23.November 2010
very good...........
James
Commented on 15.December 2010
Need some more examples to explain
Josh
Commented on 15.December 2010
@Wazzzuup
A User Control is simply a composite of existing controls, so you cannot stylize it. However, you can style the individual elements within that User Control.
Taz
Commented on 25.February 2011
It's worth noting the Usercontrol will inherit the global application theme if your using one, that means you can apply your theme globally to the entire application. We used Usercontrols heavily as it's a must for LOB applications.

(at applictaion OnStartup overrides)
Application.Current.Resources.Source = New Uri("/<my namespace>.Resource;Component/Themes/" + My.Settings.AppTheme, UriKind.Relative)
suresh
Commented on 8.March 2011
It's good
B Dinesh
Commented on 17.March 2011
I need Example code for user Control
Fred
Commented on 22.March 2011
Pretty good, but needs much more elaboration. Thanks for the good site though!
Syamala
Commented on 11.April 2011
Nice but I need more examples
Marius
Commented on 4.May 2011
That clarifies that question. Thanks!
Marius
Commented on 4.May 2011
That clarifies that question. Thanks!
sathish
Commented on 18.May 2011
thanks
sathish
Commented on 18.May 2011
thanks
Parshuram...
Commented on 2.June 2011
Thanks
Mamtha
Commented on 22.July 2011
Simple explanation and spot-on..!
Cheers
Arinal
Commented on 28.July 2011
thanks
asdfdfasdf
Commented on 28.July 2011
a s f g h j k l
Naveen
Commented on 29.July 2011
Thanks Good Inormation
Naveen
Commented on 29.July 2011
Thanks Good Inormation
Naveen
Commented on 29.July 2011
Thanks Good Inormation
Md. Nasim Uddin
Commented on 14.August 2011
Nice document . Thanks
Shashi
Commented on 25.August 2011
Really nice information. Thanks.
saranya
Commented on 14.September 2011
Good,But I need Example

Name
E-Mail (optional)
Comment