You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I have an object with a property of type Wrapper<T> and want to use it in XAML like MyProperty="SomeValue" a TypeConverter is required.
Here, the TypeConverter implementation would look like :
publicclassWrapperConverter:TypeConverter{privatereadonlyTypewrappedType;publicWrapperConverter(TypewrappedType){this.wrappedType =wrappedType;}publicoverrideboolCanConvertFrom(ITypeDescriptorContextcontext,TypesourceType){returnsourceType==typeof(string)||base.CanConvertFrom(context, sourceType);}publicoverrideobjectConvertFrom(ITypeDescriptorContextcontext,CultureInfoculture,objectvalue){TyperetType=typeof(Wrapper<>).MakeGenericType(wrappedType);varconversionResult= TypeDescriptor.GetConverter(wrappedType).ConvertFrom(context, culture, value);varret= Activator.CreateInstance(retType);// var ret = new Wrapper<T>();
retType.GetProperty(nameof(Wrapper<object>.Wrapped)).SetValue(ret, conversionResult);// ret.Wrapped = conversionResult;returnret;}}
The issue is, if I apply[TypeConverter(typeof(WrapperConverter))] to my Wrapper<T> class, the XAML parser doesn't find the constructor for WrapperConverter (since it has no default constructor) and hence throws a XamlParseException.
Most likely solution to me
Now if I implement a TypeDescriptionProvider and a CustomTypeDescriptor :
I can control the WrapperConverter's instantiation and use my WrapperConverter's parameterized constructor and convert strings to Wrapper<T> instances by applying [TypeDescriptionProvider(typeof(WrapperTypeDescriptionProvider))] to my Wrapper<T> class :
The only problem with this solution is that WPF's xaml parser doesn't seem to look for a TypeDescriptionProviderAttribute but only a TypeConverterAttribute.
The text was updated successfully, but these errors were encountered:
nalka0
changed the title
Support for TypeDescriptionProvider when resolving the TypeConverter used to parse xaml files
Support for TypeDescriptionProvider when resolving the TypeConverter used while parsing xaml files
May 11, 2021
Thanks, @nalka0. Please don't assign bugs directly to WPF developers. They are assigned based on priority.
@ryalanms SamBent is the default assignee when doing "new issue" => "feature request", and the only way I can change this is removing the assignees=SamBent part of the URL, I think there's a setting that you'd like to change for the feature request button?
Issue to fix
Considering the following class :
If I have an object with a property of type
Wrapper<T>
and want to use it in XAML likeMyProperty="SomeValue"
a TypeConverter is required.Here, the TypeConverter implementation would look like :
The issue is, if I apply
[TypeConverter(typeof(WrapperConverter))]
to myWrapper<T>
class, the XAML parser doesn't find the constructor forWrapperConverter
(since it has no default constructor) and hence throws aXamlParseException
.Most likely solution to me
Now if I implement a TypeDescriptionProvider and a CustomTypeDescriptor :
I can control the
WrapperConverter
's instantiation and use myWrapperConverter
's parameterized constructor and convert strings toWrapper<T>
instances by applying[TypeDescriptionProvider(typeof(WrapperTypeDescriptionProvider))]
to myWrapper<T>
class :will output :
The only problem with this solution is that WPF's xaml parser doesn't seem to look for a TypeDescriptionProviderAttribute but only a TypeConverterAttribute.
The text was updated successfully, but these errors were encountered: