Tags


Type Converters

So how does a XAML parser or compiler find an appropriate type converter for a property value? By looking for a System.ComponentModel.TypeConverterAttribute custom attribute on the property definition or on the definition of the property’s data type.
For example, the BrushConverter type converter is used when setting Button’s Background property in XAML because Background is of type System.Windows.Media.Brush, which has the following custom attribute:
[TypeConverter(typeof(BrushConverter)), …]
public abstract class Brush : …
{

}

On the other hand, the FontSizeConverter type converter is used when setting Button’s FontSize property because the property (defined on the base Control class) has the following custom attribute:
[TypeConverter(typeof(FontSizeConverter)), …]
public double FontSize
{
get { … }
set { … }
}
In this case, marking the type converter on the property is necessary because its data type (double) is too generic to always be associated with FontSizeConverter. In fact, in WPF, double is often associated with another type converter, LengthConverter.

Advertisements