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 Wednesday, January 02, 2008 10:00 AM by jesseliberty

Comments

# TypeConverters: There's not enough TypeDescripter.GetConverter in the world

Saturday, May 10, 2008 12:54 AM by Scott Hanselman's Computer Zen

# TypeConverters: There's not enough TypeDescripter.GetConverter in the world

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

Saturday, May 10, 2008 1:39 AM by ASPInsiders

# Windows &raquo; Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? In Praise of ???

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

# Windows &raquo; Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows &#8230;

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

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

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

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

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

# Windows &raquo; Windows ?? Windows ?? Windows ?? Windows ?? Windows ?? Windows &#8230;

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

# Post: 129

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

Monday, May 12, 2008 4:50 PM by Mirrored Blogs