Silverlight Custom Control – I

This article is my first about creating Custom Controls in Silverlight 2.
So let’s build a simple one :
You need :

  • A .cs file, named with your new Custom Control name
  • A directory named Themes
  • A generic.xaml file in it (just delete generic.xaml.cs if it was created)

Silverlight Custom Control Tree in Visual Studio

A basic custom control .cs file (the logic part):

using System.Windows.Controls;
namespace Custom_Control_1
    public class EditableTextBlock : Control
        public EditableTextBlock()
            DefaultStyleKey = typeof(EditableTextBlock);

A basic custom control generic.xaml file (the graphic part):

    <Style TargetType="controls:EditableTextBlock">
        <Setter Property="Template">
                <ControlTemplate TargetType="controls:EditableTextBlock">
                    <Grid Background="AliceBlue">

Now, to test, you need to add the xmlns of your namespace, and add the usercontrol :

<UserControl x:Class="Custom_Control_1.Page"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">

In the next article, we’ll see how to bind properties with TemplateBinding.
Don’t forgot to read the MSDN !

This entry was posted in silverlight and tagged . Bookmark the permalink.

One Response to Silverlight Custom Control – I

  1. Pingback: {Dev Tricks} » Blog Archive » Silverlight Custom Control - II : Properties

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>