When you set an attribute value in XAML, the initial type of that value is String. Even other primitives such as Double are initially strings to a XAML processor, although the conversion to other nonstring primitive values or from named values of an enumeration is relatively straightforward because of built-in type conversions.
A XAML processor needs two pieces of information in order to process an attribute value. The first piece of information is the value type of the property that is being set. Any string that defines an attribute value and that is processed in XAML must ultimately be converted or resolved to a value of that type. If the value is a primitive, a direct conversion of the string is attempted. If the value is an enumeration, the string is used to check for a name match in that enumeration. If the value is neither a primitive or an enumeration, then the type in question must be able to provide an instance of the type, or a value, based on a converted string.
A special case is a markup extension. Markup extension usages must be handled by a XAML processor prior to checking for property type and other considerations. Generally, the purpose of a markup extension is to process a string and return an object. If the object returned is a type match for the property, the markup extension supplies a value for a property in a manner that avoids any type conversion taking place outside of the markup extension implementation code. One common situation where a markup extension is necessary is to make a reference to an object that already exists (at best, a stateless type converter could only generate a new instance, which might not be desirable). For more information on markup extensions, see Markup Extensions and XAML.
TypeConverter
If the value is not a primitive type or enumeration, and there is no markup extension usage, then there must be some means of converting a String to the appropriate value or a new instance when XAML is processed. This is the role of the TypeConverter implementation. TypeConverter defines four members that are relevant for converting to and from strings for XAML processing purposes:
Of these, the most important method is ConvertFrom. This method converts the input string to the required object type.
The next most important method is ConvertTo. If a WPF application is converted to a markup representation (for instance, if it is saved to XAML), ConvertTo is responsible for producing a markup representation.
CanConvertTo and CanConvertFrom are support methods that are used when a service queries the capabilities of the TypeConverter implementation. You must implement these methods to return true for certain cases, particularly for the String type.