Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF ControlTemplate vs UserControl

I've recently made an UserControl, which took quite a long time, because I had to work with custom Dependency Properties and so on...

Anyways, it was just a bunch of 3 controls: TextBox, Popup with Hierarchical Tree.

Now I realized I could probably write a ControlTemplate only. Hence what is the benefit of using UserControl?

like image 373
theSpyCry Avatar asked Jul 15 '09 12:07

theSpyCry


People also ask

What is the difference between user control and custom control in WPF?

When you have a rapid and fixed content in your UI, use UserControl . When you want to separate some basic functionality of your main view to some smaller pieces with reusability, use UserControl . When you want to use your control in different projects and each project may want to change the look, use CustomControl .

How do I customize WPF controls?

Create a new WPF project and then right-click on your solution and select Add > New Item... It will open the following window. Now select Custom Control (WPF) and name it MyCustomControl. Click the Add button and you will see that two new files (Themes/Generic.

What is control template in WPF?

The ControlTemplate contains the tree of elements that define the desired look. After you define a ControlTemplate you can attach it to any Control or Page by setting it's TemplateProperty. In this example I am also showing you how to use Triggers with ControlTemplate.

What is custom control and user control in C#?

UserControl : A control which can reuse the Components in the Applications. The control can be defined in both Xaml and Code-Behind. CustomControl : An UserInterface element that have a distinct behavior which is said as CustomControl.


1 Answers

There are three cases to consider here: UserControl, ControlTemplate, and custom Control. (I'm guessing a DataTemplate needs no explanation)

A custom Control is something you provide when you create base functionality of a new UI component. There are various pros and cons for this, but for example, if you want custom selection behaviour of an ItemsControl, you could best do it by subclassing Selector or MultiSelector (the wpftoolkit DataGrid does this). Also, if you want an object which would contain a new DependencyProperty, you will in most cases derive from Control.

The wpf principle contained here is the "lookless" control paradigm, or "be sure to expect someone templating your Control, or at least make it behave nicely in your own template scenario". Custom Controls are usually created with reusability in mind, often as parts of framework dlls.

A ControlTemplate is essentially a description of a replacement visual tree, and can be set either explicitly on FrameworkElements, or as a part of a Style. This is the option you should aim at when your goal is primarily to make an application and be done with it. You can do almost anything with a ControlTemplate visually, if you are able to get the bindings and triggers (and the possible containing Style itself) right. All this can be declared as a resource an reused, to give your application a common "theme".

A UserControl is a self-contained composite control, with parts individually editable in the designer, and is best used if you need to see your components and manage them in the designer. A ControlTemplate, on the other hand, will not expose its components for manipulation in the designer (although it will be visible). You usually create a UserControl for a Customer details page, or a Product display browser, or any case where you don't want to create a full-blown Control, but want detailed view with full designer support.

A special case here is if you use the MVVM pattern. Many great MVVM implementations use UserControls as Views, and ControlTemplates and Styles as resources used by those views. MVVM practice also minimizes the need for a custom Control, and has many other benefits.

(For more information on MVVM, among many others, Google for Josh Smith, Sacha Barber and Karl Shifflett's fantastic articles)

like image 111
Kenan E. K. Avatar answered Oct 13 '22 02:10

Kenan E. K.