Bookmark and Share Share...    Subscribe to this feed Feed   About me...


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.




 Comments on this article

Show all comments
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

Name
E-Mail (optional)
Comment
About Christian Moser