Custom vs User control

Louro picture Louro · Jun 28, 2012 · Viewed 46.2k times · Source

I've been reading some explanations about the difference between User and Custom Controls, for example this: http://www.wpftutorial.net/CustomVsUserControl.html

I want to create, for example, a simple composition of a datagrid with 2 comboboxes which are responsible to change the values from the datagrid's items. I want to create a specific control for this because I'm going to use it a lot of times. I would like to implement the logic behind and then in the xaml invocation I only have to specify the itemsSource.

For this example should I create a User or Custom control? Since I will have properties and logic, should I have a viewmodel for this control?

EDIT: Do you know some articles with clear conceptual separation between these 2 options?

Answer

Tilak picture Tilak · Jun 28, 2012

Choice is not only between user control and custom control, but among user control, custom control, customizing control template, customizing data template, header template (for collection based controls), attached properties. Refer to Control Authoring overview

I go by following order of consideration

  1. Attached Properties : If functionality can be achieved, I use attached properties. Example, Numeric text box.

  2. Control Template : When requirement can be fulfilled by customizing the control template, I use this. Example, circular progress bar.

  3. Custom control: If control template cannot do it, I use custom control. Provided I need to customize/extend already present control. Example providing Sorting, Filtering based on header row in GridView (GridView is present in metro apps, used just to illustrate the example)

  4. User control: Least preferred one. Only when composition is required, and I am unable to do it using custom control. Like in your example, 2 Combobox, and 1 datagrid. User controls does not provide seamless lookless feature that can be leveraged through custom control or control template.