Over a million developers have joined DZone.

My Experiences in Silverlight 4 : Writing re-usable Controls

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

These days I am working on Silverlight and creating Custom controls which would be re-usable in another projects. And when you set out to write re-usable Silverlight controls, Silverlight toolkit is the best code to look up to.

When you download the Silverlight toolkit. The toolkit installer also copies source code of the toolkit at           “%Program Files%\Microsoft SDKs\Silverlight\v4.0\Toolkit\Apr10\Source” folder. When you start exploring the source code, you will notice a peculiar way the source code for toolkit is arranged. Every control in toolkit is written as a content control and has a resource dictionary that has a default style.


So, inspired by this style of writing custom controls, I set out. Now the reason why you want write a custom control is you require certain custom fields and a custom style and behaviors. So when you define these controls and the style, you also want your custom properties to appear in your style(Or else why would you define those additional controls. Typically your style for your control would look something like this.


and the your custom control would look something like this.

<ResourceDictionary   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  <Style TargetType="my:CustomControl1">
    <Setter Property="Foreground" Value="#FF000000" />
    <Setter Property="BorderBrush" Value="#FFEAEAEA"/>
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="Template">
        <ControlTemplate TargetType="my:CustomControl1">
          <Grid x:Name="Root">
              	BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}"
              	BorderBrush="{TemplateBinding BorderBrush}">
						<TextBlock Text={TemplateBinding MyCustomProperty1}

And your .cs file would look something like this. Notice that there is a peculiar way I chose to name the Dependency Property. You might want to follow this pattern (i.e. your DependencyProperty should be named your Variabale Name suffixed with “Property”.

You must adhere to this Naming convention if you intend to use Blend in your Project. (If your Dependency Property is named otherwise, You might not be able to Use Blend to set these template Bindings.)

  public class MyCustomControl1 : ContentControl

			public string MyCustomProperty1
            get { return (ExpandDirection)GetValue(MyCustomProperty1Property); }
            protected internal set
            { SetValue(MyCustomProperty1Property, value); }

        public static readonly DependencyProperty MyCustomProperty1Property=
                        new PropertyMetadata(ExpandDirection.Down, OnExpandDirectionPropertyChanged));

Now my control should show the value I set in MyCustomProperty1 on the UI right?. Something like this:

<Grid x:Name="LayourRoot">
	<my:CustomControl1  MyCustomProperty1="Hello Content Control"/>

But this does-not seem to happen. After searching for sometime on Silverlight .Net forums, I could not exactly find out what I was doing wrong here. After some Binging on Bing and Binging on Google Smile with tongue out . I found that for custom properties, TemplateBinding doesnot seem to work the way I expect it to work.

So instead as per the suggestions on the Silverlight .Net forums, I modified the template of the control by replacing TemplateBinding. Now My TextBlock Markup looks something like this.

<TextBlock Text={Binding RelativeSource={RelativeSource TemplatedParent},Path=MyCustomProperty1} />

Now My Template binding works as I expect it to work. Essential what has changed in the previous representation and the new representation is still a matter of research for me. Will Keep posting more about this as and when I discover more about it.

If I have made any mistakes, feel free to write Comments about what I am mistaken about. I’ll Happily Correct it :)

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.


Published at DZone with permission of Sudheendra Kovalam. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}