Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between styles and control templates

Tags:

silverlight

I'd like to know what the differences between an Style (for a control) and a control template are. Best regards,

Gonzalo

like image 963
Gonzalo Méndez Avatar asked Feb 24 '10 17:02

Gonzalo Méndez


People also ask

What is difference between styles and templates?

Styles keep your formatting consistent within a document. Themes keep your look and feel consistent across multiple documents. Templates allow you to re-use text, and keep your look and feel consistent across multiple documents.

What is control template?

The ControlTemplate allows you to specify the visual structure of a control. The control author can define the default ControlTemplate and the application author can override the ControlTemplate to reconstruct the visual structure of the control.

What are styles in WPF?

Styles provide us the flexibility to set some properties of an object and reuse these specific settings across multiple objects for a consistent look. In styles, you can set only the existing properties of an object such as Height, Width, Font size, etc. Only default behavior of a control can be specified.

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.


Video Answer


1 Answers

A style controls the individual properties of a control. For instance, a button style can say, "For every button, use this background." A style is changing a single property on a control.

A control template is actually handling how the control displays its bound data. Instead of saying, "I want to override a control's properties," you are assembling together other smaller controls into a single control that can present different views of the bound data.

Previously in WinForms, if you wanted to write a custom list box (say that had an icon next to each item), you had to inherit from the ListView control and override the painting methods. This involved a ton of experimentation - huge pain. With WPF templates, you can use XAML to construct smaller controls together and bind them to different properties of the parent control. You are actually defining the Visual Tree for the control.

See this article for an in-depth explanation by Charles Petzold.

like image 56
Jordan Parmer Avatar answered Sep 22 '22 04:09

Jordan Parmer