Using Styles and Templates in WPF


Isn't it fun,

to instantly listen to a blog on the go? PLAY !

 
 

templates_wpf

WPF styling and templating is a feature which helps developer and designers to offer attractive look to the User Interface by customizing application appearance, we need one templating model.It is the best feature that is introduced by WPF. It gives us permission to make a change in control, for example you can add image into textbox and ComboBox. You can do lot more things by using style and template.

WPF provides Data Binding feature and it separates the design and logic. It creates .xaml file for design and .cs (C#) file for logic which is also known as Back-end. The developer and designer can work at the same time for the same UI.

Table of Content

The template defines the structure of the control and style defines its visual appearance. We can modify the default control template. There are many third-party tools for it.

Style

Style is used when you want you to want to apply the same property repeatedly in your application for example font size, fore-color, Background color, Border, etc. For this, you should not have to write the code repeatedly. Instead of that you can write the code once and use it everywhere like inheritance property and you can also modify it. Let see this by example.

.xaml file


<grid>
<stackpanel margin="70">
<stackpanel.resources><style targettype="TextBlock" type="text/css"><Setter Property="Foreground" Value="Blue" />
<Setter Property="FontSize" Value="40" />
<Setter Property="Background" Value="Purple"/></style>
</stackpanel.resources>
<textblock>Hello India</textblock>
<textblock>Hello Gujarat</textblock>
<textblock foreground="Red">Hello Rajkot</textblock>
</stackpanel>
</grid>

Output:

Picture1

As we have seen in the example that you can define the style once and you can use it as many times you want in the application and we have also done the change in “Hello Rajkot” that also given permission for it.

You can apply style in many different ways such as:

  1. Local child control style
  2. Window-wide style
  3. Application-wide style
  4. Explicitly using style

You can declare a style as a resource in the resources section in XAML file. If you declare style in the root element then you can use it whenever you want. Now, let's see how many ways you can apply style.


Local child control style

As we have seen in the previous example that the property which is used in the control can also be shared with another child control which is known as the local child control style.


Window-wide style

As we have seen in the example you have to use style into the stackpanel but you can’t use the style outside the stackpanel.

If you want to give style to the XAML file you have to use tag. Which is shown in the below example.

.xaml


<window.resources><style targettype="TextBlock" type="text/css"><Setter Property="Foreground" Value="Blue" />
<Setter Property="FontSize" Value="40" />
<Setter Property="Background" Value="Purple"/></style>
</window.resources>
<grid>
<stackpanel margin="10">
<textblock>Hello India</textblock>
<textblock>Hello Gujarat</textblock>
<textblock foreground="Red">Hello Rajkot</textblock>
</stackpanel>
</grid>
Output:

Picture2

The code is different but the output is the same in both you can use local child control style in stackpanel only. But in the case of window-wide style, you can use it everywhere you want in the window. Which you can see from the code and output.


Application-wide style

Application wide style is used to style the application you can use it throughout the application.

If you are using a visual studio this style is already created in the App.xaml file by default. Only you have to write the code for your style.

If you write the code in then it will apply the same style for the entire project which is explained in the example.

.xaml



App.xaml
<application.resources><style targettype="TextBlock" type="text/css"><Setter Property="Foreground" Value="Blue" />
<Setter Property="FontSize" Value="40" />
<Setter Property="Background" Value="Purple"/></style>
</application.resources>

MainWindow.xaml
<grid>
<stackpanel margin="10">
<textblock>Hello India</textblock>
<textblock>Hello Gujarat</textblock>
<textblock foreground="Red">Hello Rajkot</textblock>
</stackpanel>
</grid>
Output:

Picture3

As we have seen in the example that we have to create the style once and we can used it throughout the project. You can use it as many files and you can use it many times you want to which will remain same and you can also change/ modify it if you want.

You can make changes in specific control which will not affectanother control. It is one of the best features.


Explicitly using style

We have so many controls in our project that we have to use different styles but some controls do not need style so you can keep it as it is.

As we have seen that if you declare the style once it will apply throughout the project but if we don’t want the style in the same control then you can use explicitly using style.

By using x:key property on the style you are saying WPF that you want to use the style on a particular control. Now, let’s see the example.

Example:


<window.resources><style targettype="TextBlock" type="text/css" x:key="Mystyle"><Setter Property="Foreground" Value="Blue" />
<Setter Property="FontSize" Value="40" />
<Setter Property="Background" Value="Purple"/></style>
</window.resources>
<grid>
<stackpanel margin="10">
<textblock>Hello India</textblock>
<textblock style="{StaticResourceMystyle}">Hello Gujarat</textblock>
<textblock>Hello Rajkot</textblock>
</stackpanel>
</grid>
Output:

Picture4

In this example, if you have noticed that by using x:key=” Mystyle” in textbox only in that textbox the style has been applied. You can see that another two textboxesdo not have that style like this you can also set x: key into your project to set the style in a particular control.

Template

Window presentation foundation gives you permission to completely change the control UI. You can change it by using template control and you can create a visual tree by using a template.

There are two types of templates:

  1. Control template.
  2. Data template.


Control Template

It shows the shape and behavior of the control like button shape, Button event, any other event, etc. which you can change easily.

Like normal control it is either square or rectangle but if you want to change the shape of a button then you can do it by using a template just like the following image.

Picture5


Data template

A data template defines the structure of a collection of data which gives flexibility to the format and also defines the presentation of the data on any UI element.

It is used for data-related controls like Combobox, Listbox, etc.

Conclusion

WPF styling gives us permission to easily apply the style and reuse the code for a particular application you can create once and use the style all over the project and you can also create a style by using property Window.Resource, Application.Resource, X: key, etc.Style and template are the features of WPF which are used for fore color, background color, alignment likewise template is used for shape, click event, data load, etc.

templates_wpf

WPF styling and templating is a feature which helps developer and designers to offer attractive look to the User Interface by customizing application appearance, we need one templating model.It is the best feature that is introduced by WPF. It gives us permission to make a change in control, for example you can add image into textbox and ComboBox. You can do lot more things by using style and template.

WPF provides Data Binding feature and it separates the design and logic. It creates .xaml file for design and .cs (C#) file for logic which is also known as Back-end. The developer and designer can work at the same time for the same UI.

Table of Content

The template defines the structure of the control and style defines its visual appearance. We can modify the default control template. There are many third-party tools for it.

Style

Style is used when you want you to want to apply the same property repeatedly in your application for example font size, fore-color, Background color, Border, etc. For this, you should not have to write the code repeatedly. Instead of that you can write the code once and use it everywhere like inheritance property and you can also modify it. Let see this by example.

.xaml file


<grid>
<stackpanel margin="70">
<stackpanel.resources><style targettype="TextBlock" type="text/css"><Setter Property="Foreground" Value="Blue" />
<Setter Property="FontSize" Value="40" />
<Setter Property="Background" Value="Purple"/></style>
</stackpanel.resources>
<textblock>Hello India</textblock>
<textblock>Hello Gujarat</textblock>
<textblock foreground="Red">Hello Rajkot</textblock>
</stackpanel>
</grid>

Output:

Picture1

As we have seen in the example that you can define the style once and you can use it as many times you want in the application and we have also done the change in “Hello Rajkot” that also given permission for it.

You can apply style in many different ways such as:

  1. Local child control style
  2. Window-wide style
  3. Application-wide style
  4. Explicitly using style

You can declare a style as a resource in the resources section in XAML file. If you declare style in the root element then you can use it whenever you want. Now, let's see how many ways you can apply style.


Local child control style

As we have seen in the previous example that the property which is used in the control can also be shared with another child control which is known as the local child control style.


Window-wide style

As we have seen in the example you have to use style into the stackpanel but you can’t use the style outside the stackpanel.

If you want to give style to the XAML file you have to use tag. Which is shown in the below example.

.xaml


<window.resources><style targettype="TextBlock" type="text/css"><Setter Property="Foreground" Value="Blue" />
<Setter Property="FontSize" Value="40" />
<Setter Property="Background" Value="Purple"/></style>
</window.resources>
<grid>
<stackpanel margin="10">
<textblock>Hello India</textblock>
<textblock>Hello Gujarat</textblock>
<textblock foreground="Red">Hello Rajkot</textblock>
</stackpanel>
</grid>
Output:

Picture2

The code is different but the output is the same in both you can use local child control style in stackpanel only. But in the case of window-wide style, you can use it everywhere you want in the window. Which you can see from the code and output.


Application-wide style

Application wide style is used to style the application you can use it throughout the application.

If you are using a visual studio this style is already created in the App.xaml file by default. Only you have to write the code for your style.

If you write the code in then it will apply the same style for the entire project which is explained in the example.

.xaml



App.xaml
<application.resources><style targettype="TextBlock" type="text/css"><Setter Property="Foreground" Value="Blue" />
<Setter Property="FontSize" Value="40" />
<Setter Property="Background" Value="Purple"/></style>
</application.resources>

MainWindow.xaml
<grid>
<stackpanel margin="10">
<textblock>Hello India</textblock>
<textblock>Hello Gujarat</textblock>
<textblock foreground="Red">Hello Rajkot</textblock>
</stackpanel>
</grid>
Output:

Picture3

As we have seen in the example that we have to create the style once and we can used it throughout the project. You can use it as many files and you can use it many times you want to which will remain same and you can also change/ modify it if you want.

You can make changes in specific control which will not affectanother control. It is one of the best features.


Explicitly using style

We have so many controls in our project that we have to use different styles but some controls do not need style so you can keep it as it is.

As we have seen that if you declare the style once it will apply throughout the project but if we don’t want the style in the same control then you can use explicitly using style.

By using x:key property on the style you are saying WPF that you want to use the style on a particular control. Now, let’s see the example.

Example:


<window.resources><style targettype="TextBlock" type="text/css" x:key="Mystyle"><Setter Property="Foreground" Value="Blue" />
<Setter Property="FontSize" Value="40" />
<Setter Property="Background" Value="Purple"/></style>
</window.resources>
<grid>
<stackpanel margin="10">
<textblock>Hello India</textblock>
<textblock style="{StaticResourceMystyle}">Hello Gujarat</textblock>
<textblock>Hello Rajkot</textblock>
</stackpanel>
</grid>
Output:

Picture4

In this example, if you have noticed that by using x:key=” Mystyle” in textbox only in that textbox the style has been applied. You can see that another two textboxesdo not have that style like this you can also set x: key into your project to set the style in a particular control.

Template

Window presentation foundation gives you permission to completely change the control UI. You can change it by using template control and you can create a visual tree by using a template.

There are two types of templates:

  1. Control template.
  2. Data template.


Control Template

It shows the shape and behavior of the control like button shape, Button event, any other event, etc. which you can change easily.

Like normal control it is either square or rectangle but if you want to change the shape of a button then you can do it by using a template just like the following image.

Picture5


Data template

A data template defines the structure of a collection of data which gives flexibility to the format and also defines the presentation of the data on any UI element.

It is used for data-related controls like Combobox, Listbox, etc.

Conclusion

WPF styling gives us permission to easily apply the style and reuse the code for a particular application you can create once and use the style all over the project and you can also create a style by using property Window.Resource, Application.Resource, X: key, etc.Style and template are the features of WPF which are used for fore color, background color, alignment likewise template is used for shape, click event, data load, etc.