Ideally, every class you want to use in XAML will have a no-argument constructor. If it
does, the XAML parser can create the corresponding object, set its properties, and attach any
event handlers you supply. XAML doesn??™t support parameterized constructors, and all the elements
in WPF elements include a no-argument constructor. Additionally, you need to be able
to set all the details you want using public properties. XAML doesn??™t allow you to set public
fields or call methods.
If the class you want to use doesn??™t have a no-argument constructor, you??™re in a bit of a
bind. If you??™re trying to create a simple primitive (such as a string, date, or numeric type), you
can supply the string representation of your data as content inside your tag. The XAML parser
will then use the type converter to convert that string into the appropriate object. Here??™s an
example with the DateTime structure:
10/30/2010 4:30 PMThis works because the DateTime class uses the TypeConverter attribute to link itself to
the DateTimeConverter. The DateTimeConverter recognizes this string as a valid DateTime
object and converts it. When you??™re using this technique you can??™t use attributes to set any
properties for your object.
If you want to create a class that doesn??™t have a no-argument constructor and there isn??™t
a suitable type converter to use, you??™re out of luck.
Pages:
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156