I want to understand how WPF converts the string value (Red
) to the corresponding SolidColorBrush
in the case below?
How can we do the same with our custom DependencyProperty
?
<Button Background="Red" />
This is done using type converters, see TypeConverters and XAML.
This topic introduces the purpose of type conversion from string as a general XAML language feature. In the .NET Framework, the TypeConverter class serves a particular purpose as part of the implementation for a managed custom class that can be used as a property value in XAML attribute usage. If you write a custom class, and you want instances of your class to be usable as XAML settable attribute values, you might need to apply a TypeConverterAttribute to your class, write a custom TypeConverter class, or both.
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 neither a parser-understood primitive nor 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. This is done by indicating a type converter class. The type converter is effectively a helper class for providing values of another class, both for the XAML scenario and also potentially for code calls in .NET code.
WPF has a few built-in type converters like in case of a Brush
, the BrushConverter
. You can see how the brush converter is implemented in .NET e.g. in the reference source or on GitHub for .NET Core.
As you can see from the documentation of Brush
, it specifies an attribute for the converter.
[System.ComponentModel.TypeConverter(typeof(System.Windows.Media.BrushConverter))]
[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
public abstract class Brush : System.Windows.Media.Animation.Animatable, IFormattable
You do not create a type converter for a dependency property, but its underlying type, like Brush
. The documenation shows in detail how to create type converters for custom types. In essence:
Create custom type converter that dervies from TypeConverter
.
public sealed class MyCustomTypeConverter : TypeConverter
{
// ...conversion methods.
}
Implement the following methods.
Finally, apply the TypeConverterAttribute
to your custom type.
In order for your custom type converter to be used as the acting type converter for a custom class by a XAML processor, you must apply the TypeConverterAttribute to your class definition. The ConverterTypeName that you specify through the attribute must be the type name of your custom type converter. With this attribute applied, when a XAML processor handles values where the property type uses your custom class type, it can input strings and return object instances.
[TypeConverter(typeof(MyCustomTypeConverter ))]
public class YourCustomType
{
// ...code of your custom type.
}
Following these steps, the XAML parser will automatically be able to convert strings to your type.
The BrushConverter, mentioned above, is a good example of a TypeConverter
implementation to start from.