Home > WPF > WPF: Custom Control VS User Control

WPF: Custom Control VS User Control

There are two concepts in WPF projects which are quite confusing: User Control and Custom Control. Which are available through [Add a new item] menu:

ccontrol

The best description which I found on the web is on the Martins blog:

It seems to have collection of controls which provide a combined functionality User Control is the way to go, but if you want to create a new button control or text box with new behavior you need to look at custom control.

Custom Control
1. The purpose of it is to enhance the existing control,
2. It supports theming for consumers, which means the consumers can style it in whatever way they like,
3. It is perfect to be 3rd-party control, especially because of point 2.

Whereas User Control
1. Its purpose is to compose controls into one piece,
2. It does not support theming for consumers. Therefore, the consumers can not restyle it,
3. It better stay with the consumer application in case the consumer needs to change its style somehow.

Ref:http://wangmo.wordpress.com/2007/09/28/user-controls-vs-custom-controls/

Advertisements
Categories: WPF
  1. September 23, 2008 at 3:40 am

    Hi,
    thank you for this good comparison.
    I’ve made a summary with two examples on my website:

    http://www.wpftutorial.net/The+differences+between+CustomControl+and+UserControl.htm

    Greetings
    Chris

  2. Cameron Thompson
    March 28, 2009 at 6:08 am

    something of note: a custom control is not the only way to enhance a control:
    Add a user control, change the root tags to Button, and the Class to extend Button instead of UserControl.

    This can be done with any control. And gives you the benefit of xaml and codebehind for all your extending purposes.

    I tend to group this under the User Control category (even though it technically is not since it does not extend the UserControl class).

    Thus the only use I see for using the Custom Control with its added complexity is for theaming or making a 3rd party control. And I wonder if those things may be possible with a User Control or extending a control as I have just described.

  3. wazzzuup
    March 31, 2009 at 6:18 am

    and what if I whnt to do following:

    I have such task – to create control that union two controls (DataGrid from WPFToolkit and standard Toolbar). On a large scale, it doesn’t matter what particular controls it unions, I need to find out the common practices that can be used to build what I need. At first glance, I need something like user control, i.e composition of controls, that I can implement as a whole and reuse then. But, my task requires me to have possibility to tune my composite control in XAML. So, if I compose Toolbar and DataGrid, I want properties and events of both them would be exposed. So I could set in XAML both, for example, columns of datagrid and bars of toolbar:

    (I put spaces in tag names intentionally, because loacal parser didn’t type them for some reason)

    The only decison that suits me less or mor for now is to make custom control inherited from Datarid (as DataGrid is more significant in this pair) and redefine Template including both datagrid and toolbar. This gives me all power of datagrid but, if I want get Toolbar part, I need to do it through code, seeking it in viual and logical trees i.e. getting access o it programmatcally, which is not pretty decision.
    Please help a newbie to solve this task ) Thanks in advance…

  4. wazzzuup
    March 31, 2009 at 6:24 am

    why cant I insert my XAML code? even within or without barckets

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: