Page view counter

Did You Know That... Type Converters allow you to use property attributes in Complex Types

There are two ways to add attributes to any element in Silverlight:

  1. property attribute syntax, which takes a string
  2. Property elements

 

Silverlight provides a number of built-in type converters to allow you to use the simpler property attribute syntax, where you might otherwise need to use a property element. The most familiar example of this is that you can write

<Rectangle Name="myRect0"
       Width="100"
       Height="44"
       Canvas.Left="0"
       Canvas.Top="10"
       StrokeThickness="2"
       Stroke="Black"
       Fill="Red"
       />

A naïve view of this code, might lead you to think that the stroke and fill colors are both strings.

You can quickly disabuse yourself of this notion by adding a little bit of code to your handleLoad event handler

handleLoad: function(plugIn, userContext, rootElement) 
{
    this.plugIn = plugIn;
    var myRect = plugIn.content.FindName("myRect0");
    alert("myRect.stroke = " + myRect.Stroke);
    alert("myRect.fill = " + myRect.Fill);
}

You might expect the first alert box to say "myRect.stroke = Black" and the second alert box to say "myRect.fill= Red"

But on reflection, you realize that what is in the stroke and in the fill is not a string, but rather is a solidColorBrush object that has been placed there using the typeConverter, as shown in figure 1

 

 

TypeConverters

All of this makes a great deal more sense when you realize that the type converters are simply acting as shorthand, allowing you to avoid the more verbose but more explicit code, shown here

 

<Rectangle Name="myRect0"
       Width="100"
       Height="44"
       Canvas.Left="120"
       Canvas.Top="10"
       StrokeThickness="2"
       >
    <Rectangle.Stroke>
        <SolidColorBrush Color="Black"  />
    </Rectangle.Stroke>

    <Rectangle.Fill>
        <SolidColorBrush Color="Red" />
    </Rectangle.Fill>
</Rectangle>
Published 02 January 2008 10:00 AM by jesseliberty

Comments

# Scott Hanselman's Computer Zen said on 10 May, 2008 12:54 AM
# ASPInsiders said on 10 May, 2008 01:39 AM

While reading source today, I saw some code in the wild today that looked roughly like this (not the

# Windows » Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? In Praise of ??? said on 12 May, 2008 04:55 AM

Pingback from  Windows &raquo; Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? In Praise of ???

# Windows » Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows … said on 12 May, 2008 07:06 AM

Pingback from  Windows &raquo; Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows &#8230;

# Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows … | My Geek Solutions said on 12 May, 2008 08:32 AM

Pingback from  Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows &#8230; | My Geek Solutions

# Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows … | My Geek Solutions said on 12 May, 2008 10:03 AM

Pingback from  Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows &#8230; | My Geek Solutions

# Windows » Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows … said on 12 May, 2008 10:34 AM

Pingback from  Windows &raquo; Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows &#8230;

# Mirrored Blogs said on 12 May, 2008 04:50 PM

Post: Approved at: May-12-2008 Timeline change from Flash to Silverlight http://designwithsilverlight