Jesse Liberty - Silverlight Geek

By, For and About Silverlight Developers

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>

Comments

Scott Hanselman's Computer Zen said:

# May 10, 2008 12:54 AM

ASPInsiders said:

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

# May 10, 2008 1:39 AM

Windows PowerShell said:

I just got through reading Scott Hanselman's blog entry:&#160; TypeConverters: There's not enough TypeDescripter.GetConverter

# May 10, 2008 11:06 AM

Windows » In Praise of TypeConverters - Spread the Love said:

Pingback from  Windows &raquo; In Praise of TypeConverters - Spread the Love

# May 10, 2008 4:43 PM

Windows » Windows ?? In Praise of TypeConverters - Spread the Love said:

Pingback from  Windows &raquo; Windows ?? In Praise of TypeConverters - Spread the Love

# May 10, 2008 6:53 PM

Windows ?? In Praise of TypeConverters - Spread the Love | My Geek Solutions said:

Pingback from  Windows ?? In Praise of TypeConverters - Spread the Love | My Geek Solutions

# May 10, 2008 6:59 PM

Windows » Windows ?? Windows ?? In Praise of TypeConverters - Spread the Love said:

Pingback from  Windows &raquo; Windows ?? Windows ?? In Praise of TypeConverters - Spread the Love

# May 10, 2008 9:16 PM

Windows » Windows ?? In Praise of TypeConverters - Spread the Love | My Geek … said:

Pingback from  Windows &raquo; Windows ?? In Praise of TypeConverters - Spread the Love | My Geek &#8230;

# May 10, 2008 9:16 PM

Windows ?? In Praise of TypeConverters - Spread the Love | My Geek … | My Geek Solutions said:

Pingback from  Windows ?? In Praise of TypeConverters - Spread the Love | My Geek &#8230; | My Geek Solutions

# May 10, 2008 10:14 PM

Windows » Windows ?? Windows ?? Windows ?? In Praise of TypeConverters … said:

Pingback from  Windows &raquo; Windows ?? Windows ?? Windows ?? In Praise of TypeConverters &#8230;

# May 10, 2008 11:23 PM

Windows » Windows ?? Windows ?? In Praise of TypeConverters - Spread the Love … said:

Pingback from  Windows &raquo; Windows ?? Windows ?? In Praise of TypeConverters - Spread the Love &#8230;

# May 10, 2008 11:23 PM

Windows » Windows ?? Windows ?? In Praise of TypeConverters - Spread the Love … said:

Pingback from  Windows &raquo; Windows ?? Windows ?? In Praise of TypeConverters - Spread the Love &#8230;

# May 10, 2008 11:23 PM

Windows ?? Windows ?? In Praise of TypeConverters - Spread the Love … | My Geek Solutions said:

Pingback from  Windows ?? Windows ?? In Praise of TypeConverters - Spread the Love &#8230; | My Geek Solutions

# May 10, 2008 11:32 PM

Windows » Windows ?? Windows ?? Windows ?? Windows ?? In Praise of … said:

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

# May 11, 2008 5:55 AM

Windows » Windows ?? Windows ?? In Praise of TypeConverters - Spread the … said:

Pingback from  Windows &raquo; Windows ?? Windows ?? In Praise of TypeConverters - Spread the &#8230;

# May 11, 2008 5:55 AM

Windows » Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? In Praise of ??? said:

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

# May 12, 2008 4:55 AM

Windows » Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows … said:

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

# May 12, 2008 7:06 AM

Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows … | My Geek Solutions said:

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

# May 12, 2008 8:32 AM

Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows … | My Geek Solutions said:

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

# May 12, 2008 10:03 AM

Windows » Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows … said:

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

# May 12, 2008 10:34 AM

Mirrored Blogs said:

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

# May 12, 2008 4:50 PM