Reader Level:

Custom Control in Windows Store Apps Using C#

By Gaurav Gupta on Sep 10, 2012
In this article we are going to learn how to create and use Custom Controls in Windows Store apps using C#.

Today we will learn how to use custom controls in Windows Store Apps. In my previous article I showed you how to create and use a User Control template in Windows Store Apps. The user can also create Custom Controls in Windows Store Apps. The user can use a Custom Control the same as a User Control by including their namespaces.

The Custom Control is a control that can be used in multiple projects. It allows the user to restyle and change the default behavior when needed. A Custom Control is built with the help of the core control of Windows Store Apps such that button, image etc.

In Visual Studio 2012 RC you can create a Custom Control in Windows Store Apps using the Templated Control. Windows Store Apps provide the Templated Control item template to add a custom control within the project and use it anywhere. When you add a custom control, the app automatically creates two necessary to files to process the custom control; they are:

Theme/generic.xaml: It contains the default style and template layout for the custom control.
The class file of Custom Control: This file contains the control login if needed.

Now we are going to add a Custom Control in the Windows Store Apps project.

Step 1

Create a Windows 8 Application using C#.

Step 2

Add a Custom Control to the application project.

  1. Right-click on the Solution Explorer.
  2. Select Add -> New Item.
  3. Select Templated Control.
  4. Click on the Add button.


Step 3

Adding the Custom Control Template creates two required elements for the Custom Control.


Step 4

Open the Class file CustomControl.cs file. In the constructor of your class you must set the DefaultStyleKey to the type of your control. This enables application of the style and the template for this type whenever it is used.

namespace UserAndCustomControls


    public sealed class BasicCustomControl : Control


        public BasicCustomControl()


            this.DefaultStyleKey = typeof(BasicCustomControl);




Step 5

In the generic.xaml we write the code for the style and design for the custom control; see:





    <!-- generic.xaml can contain one or more definitions for any number of custom controls within the project -->

    <!-- a project only has one themes\generic.xaml -->

    <!-- this is the definition for our basic control -->

    <Style TargetType="local:BasicCustomControl">

        <Setter Property="Template">


                <ControlTemplate TargetType="local:BasicCustomControl">

                    <StackPanel Orientation="Vertical">

                        <ListBox >


                                <StackPanel  Orientation="Horizontal">

                                    <Image Source="Windows 8.jpg" Height="200" Width="300" Stretch="Fill"></Image>

                                    <Image Source="Windows_8.jpg" Height="200" Width="300"   Stretch="Fill"></Image>

                                    <Image Source="windows8(1).jpg" Height="200" Width="300"   Stretch="Fill">









Step 6

Here we use Custom Contol in the MainPage.XAML file. We need to add the namespace of the Custom Control. In this article the User Control is defined under the same namespace; see:


Step 7

Use the Custom Control like that:









Grid x:Name="Output" HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Row="1">

      <StackPanel HorizontalAlignment="Center">

          <TextBlock Text="Below is the result of using our custom control" />

      <!-- observe the xmlns:local scope declaration in the root node of this view -->

         <local:BasicCustomControl Width="auto" Height="auto"/>



Step 8

Now, all the work is done. Build your application and Run the MainPage.XAML page.


Gaurav Gupta
Gaurav Gupta

I am a student and Developer. I like to explore new technologies, mostly working on web development.