
The Role of XAML Type Converters
Silverlight supports several intrinsic ‘type converters’. Their job is to map simple string values into complex
objects.
Consider, for example, the act of setting the Background property to the string ‘Green’. In code, the
Background property expects a Brush-derived type. Furthermore, the Height and Width properties expect
double values, not strings. Regardless, the following XAML works as expected.
<Button Name = "myButton" Height = "100"
Width = "100" Content = "Click Me!" Background = "Green"/>
Behind the scenes, these string values (‘100’, ‘100’ and ‘Green’) are transformed into doubles and a
SolidColorBrush object via type converters. Silverlight supports numerous type converters, all of which
derive from the System.ComponentModel.TypeConverter base class. For example, the BrushConverter
type was used to map ‘Green’ into a brush object.
While you could make direct use of type converters in code, there is usually no compelling reason to do so. In
the vast majority of the cases, the correct type converter will be used at compilation time.
The Role of XAML Markup Extensions
Although the majority of property values can be assigned using strings or via property-element syntax, some
property values cannot, as in the following cases:
• You wish to assign a property value to an object created elsewhere (in code, as a logical resource,
and so forth).
• You wish to assign a property to a value calculated at runtime.
For these and other reasons, XAML supports markup extensions. In a nutshell, markup extensions are used
to set property values in nonstandard ways. Again, these nonstandard ways typically involve referring to
objects allocated elsewhere or data computed at runtime.
Like a type converter, markup extensions have a class-based equivalent in the WPF libraries. And like type
converters, these classes seldom need to be used directly in your code.
A XAML markup extension is encased within curly brackets at the time you assign a property value. Here is
a general template to follow.
<DefiningType DefiningTypeProperty = "{x:MarkUpExtension Value}" >
</DefiningType>
You will see the use of various markup extension during the remainder of this class. In the meantime, here is
an example of the {StaticResource} markup extension. We are using the x:Key token to define a resource
item for a simple style. The {StaticResource} markup extension is used to pluck out the resource by name,
in order to set the Style property of the <Border> control.
<UserControl x:Class = "SimpleSilverlightApplication.Page"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
Height = "300" Width = "300">
<UserControl.Resources>
<Style TargetType="Border" x:Key="PageBackground">
<Setter Property="BorderBrush" Value="Blue"/>
<Setter Property="BorderThickness" Value="5"/>
</Style>
</UserControl.Resources>
<Border Style="{StaticResource PageBackground}">
</Border>
</UserControl>
That wraps up your overview of the Silverlight 2.0 programming model. You will see many other examples of
XAML over the remainder of this class. Your next lab will give you a chance to build a Silverlight web plug-in
which can process XAML at runtime, to display a runtime generated UserControl.
The Role of XAML Type Converters
Table of Contents
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services