Please enable Javascript to correctly display the contents on Dot Net Tricks!

Different Types of Templates in WPF

  Author : Shailendra Chauhan
Updated On : 09 Oct 2013
Total Views : 22,154   
 

WPF enables you to change the look and feel of the WPF controls and this is achieved by Templates. Templates are used to change the look and feel of a control in WPF.

Types of Templates

  1. Control Template

    This template specifies the appearance of a Control; if a control does not have a Control Template, the Control will not appear in your application.

    For Example - When you will add the template defines as below to your application as a resource then all the buttons in the application will appear as ellipses but will still function as buttons.

    <Style TargetType="Button">
     <!--Set to true to not get any properties from the themes-->
     <Setter Property="OverridesDefaultStyle" Value="True"/>
     <Setter Property="Template">
     <Setter.Value>
     <ControlTemplate TargetType="Button">
     <Grid>
     <Ellipse Fill="{TemplateBinding Background}"/>
     <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
     </Grid>
     </ControlTemplate>
     </Setter.Value>
     </Setter>
    </Style>
    
  2. Data Template

    This template specifies a group of characteristics for how data should be displayed. This template is particularly useful when you are binding an ItemsControl such as a ListBox to an entire collection.

    For Example – The Template defined as below, is used to display the items of a ListBox. The data template contains TextBlock controls that bind to the FirstName, LastName, and Address properties.

    >Grid>
     >Grid.Resources>
     >src:Customers x:Key="customers"/>
     >/Grid.Resources>
     >ListBox ItemsSource="{StaticResource customers}" Width="350" Margin="0,5,0,10">
     >ListBox.ItemTemplate>
     >DataTemplate>
     >StackPanel Orientation="Horizontal">
     >TextBlock Padding="5,0,5,0"
     Text="{Binding FirstName}" />
     >TextBlock Text="{Binding LastName}" />
     >TextBlock Text=", " />
     >TextBlock Text="{Binding Address}" />
     >/StackPanel>
     >/DataTemplate>
     >/ListBox.ItemTemplate>
     >/ListBox>
    >/Grid>
    
What do you think?

I hope you will enjoy the tips while programming with WPF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.


YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
13 FEB
ASP.NET Core Development (online)

07:00 AM-09:00 AM IST( MON, WED, FRI)

Know More
25 JAN
Angular2 and Angular1 Development (online)

09:00 PM-10:30 PM IST(MON-FRI)

Know More
24 JAN
ASP.NET MVC with Angular2 Development (online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

Know More
20 JAN
NODEJS & MEAN Stack 2 Development (online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

Know More
15 JAN
PPC Marketing (offline)

04:00 PM-05:300 PM

14 JAN
Xamarin Forms : Build Cross-platform Apps (offline)

05:00 PM-06:30 PM IST

10 JAN
ASP.NET MVC with AngularJS Development (online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

3 JAN
ASP.NET MVC with AngularJS Development (online)

07:00 AM-09:00 AM IST

31 DEC
ASP.NET MVC with AngularJS Development (offline)

09:30 AM-11:00 AM

31 DEC
Angular2 and Angular1 Development (offline)

11:00 AM-12:30 AM

26 NOV
ASP.NET MVC with AngularJS Development (offline)

03:00 PM-04:30 PM

5 NOV
ASP.NET MVC with AngularJS Development (offline)

08:00 AM-09:30 AM

4 SEP
MEAN Stack 2 Development (offline)

11:00 AM-12:30 PM.

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+