Open-up and generalize the TypeConverter framework
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
User story: As developer, I would like to extend the WPF and XAML frameworks to accept custom content and seamlessly integrate into the XAML framework _doing the neccesary conversion_.
Example: I want to create a custom type (let’s say some SVG support) that can be used as an **ImageSource**, for example for the **Image.Source** property.
Today, I can use strings, which will automatically be converted to ImageSources as if they were URLs to the image resources. The same is true for some other types like byte array and streams. But this is limited to some predefined types.
I can create a custom **TypeConverter** on my class and the binding framework will (_mostly_) use those converters. Unfortunately, this will not work if I embed XAML directly as a value to the property. This will not work with static resources either.
Furthermore, the WPF/XAML well-known-types cache their converters and do not allow custom converters to be registered using **TypeDescriptor** and **TypeDescriptionProvider**. It is for example desirable to be able to override or augment the **ImageSourceConverter** (as well as many other converters).
Please consider implementing a hook, or even better, a global converter repository for the application, where the conversion behavior can be altered.
Example:
```xaml
```
```CSharp
[TypeConverter(typeof(SvgImageTypeConverter))]
class SvgImage : Object
{
}
class SvgImageTypeConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(ImageSource))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is SvgImage svg)
{
if (destinationType == typeof(ImageSource))
return SomehowConvertToImageSource(svg);
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
```
As a bonus, please consider making the converter framework generic. For example, let’s say we have a view-model assembly that implements the abstract states “info”, “warning” and “error” and we want to convert those to colors (or brushes). One would like to have a type-converter that can do this, but the view-model assembly where the type in question is defined has no reference to the WPF assemblies with the color class, so a type-converter on the “state” class is not an option. Likewise, the Color type is a WPF type, which we cannot modify to change the type converter. It would be desirable to have a global registry where one can define converters between, let’s say “state” and “color”. This will of-course be done in an assembly that can access both types. The example here is naïve, but should demonstrate the problem.
Contributor guide
Assessment
This issue has not been assessed yet.