The value in an XML attribute is always a plain text string. However,
object properties can be any .NET type. In the previous example, there are two properties that
use enumerations (VerticalAlignment and HorizontalAlignment), one string (FontFamily), one
integer (FontSize), and one Brush object (Foreground).
In order to bridge the gap between string values and nonstring properties, the XAML
parser needs to perform a conversion. The conversion is performed by type converters, a basic
piece of .NET infrastructure that??™s existed since .NET 1.0.
CHAPTER 2 n XAML 33
Essentially, a type converted has one role in life??”it provides utility methods that can convert
a specific .NET data type to and from any other .NET type, such as a string representation
in this case. The XAML parser follows two steps to find a type converter:
1. It examines the property declaration, looking for a TypeConverter attribute. (If present,
the TypeConverter attribute indicates what class can perform the conversion.) For
example, when you use a property such as Foreground, .NET checks the declaration of
the Foreground property.
2. If there??™s no TypeConverter attribute on the property declaration, the XAML parser
checks the class declaration of the corresponding data type. For example, the Foreground
property uses a Brush object.
Pages:
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132