Difference between Style and Template.


One day my colleague asked(the same question was asked by many people) me about the difference between Style and Template, so i though to explain the difference between these two things by writing a blog post. So that  others can also read this.

Style:

Brief:

Style can be applied to any Silverlight control which is derived from the Framework Element. With the help of style we can modify the default property values of a control for which the style is applied. We can override the property value which are defined in style when we initialize the control on UI. With the help of Style we can specify the default behavior of the control.

Detailed:

The default properties of a controls are defined in <Setter> tag which are defined in the style,

EG:

<Style TargetType=”TextBox” x:Key=”TextboxStyle”>

  <Setter Property=”Background”  Value=”White” />
      <Setter Property=”BorderThickness” Value=”1″ />
<Setter Property=”Template”>
<Setter.Value>

<Setter

</Style>

We can modify the default properties of a control by overriding the properties when we initialize the control on UI.

EX:

<TextBox Background=”Red” Style=”{StaticResource TextboxStyle}”/>

the Background property will changes to red by default it is White which is defined in the style.

Template:

Templates   Used only those elements that inherit from the Control class in the System.Windows.Control namespace.By using  Templates u can modify the structure of the Control to which the Template is applied.Templates      Values can’t be overridden by the values that are set on the control itself when it is drawn on the artboard .But , using Template Binding the property’s  value can set of a template according to the values of a properties of the control when it is drawn on the artboard.When u  Modify with Template u can access to more parts of control then styles.

EX for TemplateL

<ControlTemplate TargetType=”TextBox”>
<Grid x:Name=”RootElement”>

</Grid>
</ControlTemplate>

I hope this may give you basic idea of Template and style.

Advertisements

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