Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Style and ControlTemplate

Tags:

c#

.net

wpf

xaml

Could you tell me what is the main differences between Style and ControlTemplate ? When or why to use one or the other ?

To my eyes, they are exactly the very same. As I am beginner I think that I am wrong, thus my question.

like image 504
Stephane Rolland Avatar asked May 26 '11 09:05

Stephane Rolland


People also ask

What is ControlTemplate?

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.

How do you style XAML?

A fast way to apply styles to your controls is to right-click on a control on the Microsoft Visual Studio XAML design surface and select Edit Style or Edit Template (depending on the control you are right-clicking on).


2 Answers

In a style you set properties of a control.

<Style x:Key="MyButtonStyle" TargetType="Button">     <Setter Property="Background" Value="Red"/> </Style>  <Button Style="{StaticResource MyButtonStyle}"/> 

All buttons that use this style will have their Backgrounds set to Red.

In a template you define the UI (structure) of the control.

<ControlTemplate x:Key="MyButtonTemplate" TargetType="Button">     <Grid>         <Rectangle Fill="Green"/>         <ContentPresenter/>     </Grid> </ControlTemplate>  <Button Template="{StaticResource MyButtonTemplate}"/> 

All buttons that use this template will have a green background that cannot be changed.

Values set in a template can only be replaced by replacing the entire template. Values in a style can be replaced by setting the value explicitly when using the control. That is why is better to use the properties of the control by using TemplateBinding instead of coding values.

<ControlTemplate x:Key="MyButtonTemplate" TargetType="Button">     <Grid>         <Rectangle Fill="{TemplateBinding Background}"/>         <ContentPresenter/>     </Grid> </ControlTemplate> 

Now the template uses the value of the Background property of the button it is applied to, so it can be customized:

<Button Template="{StaticResource MyButtonTemplate}" Background="Yellow"/> 

Another useful feature is that controls can pick up a default style without having a specific style being assigned to them. You can't do that with a template.

Just remove the x:Key attribute of the style (again: you can't do this with templates). All buttons in the visual tree below the style will have this style applied.

Combining Templates and Styles is extra powerful: you can set the Template property in the style:

<Style TargetType="Button">     <Setter Property="Background" Value="Red"/>     <Setter Property="Template">         <Setter.Value>              <ControlTemplate TargetType="Button">                  <Grid>                      <Rectangle Fill="{TemplateBinding Background}"/>                      <ContentPresenter/>                  </Grid>              </ControlTemplate>         </Setter.Value>     </Setter> </Style> 
like image 160
Emond Avatar answered Oct 09 '22 22:10

Emond


No indeed you are quite wrong. Styles set properties on controls. ControlTemplate is a property shared by most controls that specify how they are rendered.

To elaborate, you can use a style to group settings for a bunch of properties so you can re-use that to standardize your controls. Styles can be set explicitly on controls or applied too all of a certain type.

Control Templates can be set by a style or set explicitly on a control to change the way it appears. All controls have default templates (and styles for that matter) that are embedded in the .net wpf assemblies. It is quite enlightening to see these and understand how the wpf developers implemented the normal versions of all controls. If you have Expression blend installed, look in its "SystemThemes" folder.

UPDATE:

To understand how Styles and ControlTemplates can "add controls". In some way or another, the ControlTemplate is the only way to define the controls a control is made up of. But, some default .net controls allow you to use controls in place of text.

For example:

<GroupBox>   <GroupBox.Header>     <CheckBox/>   </GroupBox.Header> </GroupBox> 

This "adds" a checkbox to the groupbox without changing the ControlTemplate, but this is because the default ControlTemplate for GroupBox allows anything as the Header. This is done by using special controls such as ContentPresenter.

However, sometimes the default ControlTemplate for a control doesn't allow you to change something that you want to change via properties. Then you must change the ControlTemplate.

Whether you set the Properties of a control (Content, Header, ControlTemplate, IsEnabled, etc.) directly or via a style does not matter, Styles are only a convenience.

Hopefully this answers your question more clearly.

like image 33
Andre Luus Avatar answered Oct 09 '22 21:10

Andre Luus