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
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