Control Template

Control Templates enable the look-and-feel of Controls on Windows Phone 7 to differ allowing for a flexible user experience beyond the standard, such as customising a Button look and behaviour using Visual State Manager.

Printer Friendly Download Tutorial (444KB) Download Source Code (16.4KB)

Step 1

Start Microsoft Visual Studio 2010 Express for Windows Phone, then Select File then New Project... Select "Visual C#" then "Silverlight for Windows Phone" and then "Windows Phone Application" from Templates, select a Location if you wish, then enter a name for the Project and then click OK, see below:

New Project

Step 2

A Windows Phone application Page named MainPage.xaml should then appear, see below:


Step 3

Then in the XAML Pane for MainPage.xaml, below the x:Class="ControlTemplate.MainPage" entry, type the following XAML namespace:


See below:

MainPage Namespace

Step 4

While still in the XAML Pane above the <Grid x:Name="LayoutRoot" Background="Transparent"> line type the following XAML:

  <Style x:Key="CircleButton" TargetType="Button">
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Background">
        <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
          <GradientStop Offset="0" Color="#FFFF281E"/>
          <GradientStop Offset="1" Color="#FFFA9024"/>
    <Setter Property="Template">
        <ControlTemplate TargetType="Button">
              <vsm:VisualStateGroup x:Name="CommonStates">
                <vsm:VisualState x:Name="Disabled"/>
                <vsm:VisualState x:Name="Normal"/>
                <vsm:VisualState x:Name="MouseOver"/>
                <vsm:VisualState x:Name="Pressed">
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Inner"
                      <DiscreteObjectKeyFrame KeyTime="0:0:0.0" Value="1"/>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Outer"
                      <DiscreteObjectKeyFrame KeyTime="0:0:0.0" Value="-1"/>
              <vsm:VisualStateGroup x:Name="FocusStates">
                <vsm:VisualState x:Name="Focused"/>
                <vsm:VisualState x:Name="Unfocused"/>
            <Ellipse Margin="4" Fill="{TemplateBinding Background}" 
                <ScaleTransform ScaleY="1" x:Name="Outer"/>
            <Ellipse Margin="20" Fill="{TemplateBinding Background}" 
                <ScaleTransform ScaleY="-1" x:Name="Inner"/>
            <ContentPresenter x:Name="content" HorizontalAlignment="Center" VerticalAlignment="Center"/>

See below:

ControlTemplate Style Resource XAML

Step 5

Return to the MainPage.xaml Designer View by selecting the "MainPage.xaml" Tab
Then from the Windows Phone Controls section in the Toolbox select the Button control:

Button Control

Step 6

Draw a Button on the Page by dragging the Button from the Toolbox onto the Page, then in the XAML Pane between the <Grid x:Name="ContentGrid" Grid.Row="1"> and </Grid> lines, change the "Button1" line to the following:

<Button Height="250" Width="250" Style="{StaticResource CircleButton}" Content="Tap Here" Name="HelloWorld"/>

See below:

MainPage with Button

Step 7

The Button should have the "CircleButton" Style applied and not appear as a standard Button on Windows Phone 7.
Double Click on the Button Control and type in the HelloWorld_Click method:

PageTitle.Text = "Hello World";

See Below:

Button Click Event

Step 8

Save the Project as you have now finished the Windows Phone Silverlight application. Select the Windows Phone Emulator option then Select Debug then Start Debugging or click on Start Debugging:

Start Debugging

After you do, the following will appear in the Windows Phone Emulator after it has been loaded:

Application Running

Step 9

Now tap where it says "Tap Here" and the page title will change to Hello World, see below:

Control Template

Step 10

You can then Stop the application by selecting the Visual Studio 2010 application window and clicking on the Stop Debugging button:

Stop Debugging

This is a simple example of how to Customise the Control Template of a Control such as a Button, it is possible to change the look-and-feel of other controls or add more behaviours - make it your own!


Creative Commons License

Copyright Comentsys © 2009 - 2010, All rights reserved. About | Contact | Link to Page
Valid XHTML 1.1! Valid CSS Level Double-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0 This website is ICRA rated Creative Commons License