User Controls V.S. Custom Controls

I believe most of the WPF novices are quite confused with these two concepts at the beginning. (so was I). They are indeed not very clearly and distinguishably defined. Plus, the gray area between these two makes them even more ambiguous.

Well, I am gonna give my understanding of them in more programming perspective. I am not saying mine is absolutely correct. However, I am pretty confident that it is gonna help people start to choose the right one for proper use.

User Controls:

You can take it just as a composition of different other UI controls. As most of the WPF books said, the creation of a user control is very alike the creation of a window. You might be able to understand why I said that. As matter of fact, in the reality of programming, it really is. You have a XAML file and C# class file for a user control. The class file extends UserControl class, adds the additional behaviour and properties. The XAML file encapsulates the composing controls, the styles, the templates, animations and whatever necessary for “Look&Feel”. Since it is a just composition, it is really easy to create. It does not require you for too much knowledge of WPF UI model, as long as you are able to play brick game. :p

Custom Control:

Oh yeah. This one you need to take deep breath for. :p. No, I am kidding. It is truly harder than the user control, but also truly cuter, because it gives you more space to play your magic. A custom control author must extend some certain UI controls, like Button or ItemsControl. You add the functions which you want for your control but the native one is lack of. Also you have to define the generic theme for your custom control. You store the theme as a ResourceDictionary and save it in Themes/Generic.xaml. Till then, you are done with your custom control. The creation of a custom control certainly requires you to have a better understanding of WPF UI model. You need to take some time to be really good at it.

Well, now comes the question of which one you should choose. The basic answer is I don’t know. :p. It is up to the scenario. If you are not satisfied with the original functions shipped by the native UI controls, you have to write your own custom control. But if you think each piece of the native controls is pretty good and you just need a big chunk of controls, you think about the user control. Please be advised that, since lots of things that custom controls can do can be done by user controls as well, considering the complexity of a custom control and the completion of the native controls shipped by WPF, I strongly suggest to always consider the user controls in the first place. Only if there is no possibility to reach your goal with the user control, otherwise, always choose user controls over custom controls.

UPDATE:

Lately I have just figured out the summary about the differences between Custom Controls and User Controls. I could not say it is complete, but at least it offers the clearer criteria to choose which to develop.

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.

Generally & Simply speaking, custom controls have better flexibility and reusability than user controls

About these ads

~ by Martin on September 28, 2007.

12 Responses to “User Controls V.S. Custom Controls”

  1. The question that interested me is :
    Is the User control with the inherited from the window class
    Can be display in XBAP base browser app ?

  2. I don’t understand why you said “inherited from the window class”, because they extend the UserControl class.

    Anyway, what I can tell you is user controls can be used in XBAP, without any problem.

  3. I have a user control that i would like to compile and be able to add dynamically at runtime, how should i do this? most of the properties should be exposed at c# level. do you have any suggestions?

  4. [...] The best description which I found on the web is on the Martins blog: [...]

  5. Got to this point under my own steam. Problem I have is putting a custom control within a user control. I get error in xaml when I put the namespace for the custom control in the user control “The property Content is set more than once”. Help!

  6. [...] When using WinForms, the common approach to have controls have the same look and feel or common functionality is to use user controls.  Though you certainly can do this in WPF, user controls mainly used to encapsulate behavior or group controls together for a specific purpose.  You also have custom controls which basically allows you to build a control from the ground up.  For a good article on user controls vs. custom controls read more here: http://wangmo.wordpress.com/2007/09/28/user-controls-vs-custom-controls/ [...]

  7. Nicely explained.
    Thanks :)

  8. 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!

  9. Thanks Martin. This Helped me !!!

  10. Thanks to explain programmatic perspective. :)

  11. Nice info. Thanks.

  12. User Controls V.S. Custom Controls in WPF
    http://wpftutorial.co.in/WPFCustomControlls/CustomControlsVsUserControls.aspx

Leave a Reply

Fill in your details below or click an icon to log in:

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

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: