Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now

Different Types of Templates in WPF

09 Oct 2013
Intermediate
41.5K Views
2 min read
Learn with an interactive course and practical hands-on labs

Best Free C# Course: Learn C# In 21 Days

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.

Share Article
Similar Articles
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this