It is odd in my view that Xaml can't parse all types that implement IConvertible
. (I'd love to know the reason if any of you MS lurkers care to edify?)
Here is an implementation that may help:-
public class ConvertibleTypeConverter<T> : TypeConverter where T: IConvertible
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType.GetInterface("IConvertible", false) != null;
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType.GetInterface("IConvertible", false) != null;
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
return ((IConvertible)value).ToType(typeof(T), culture);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
return ((IConvertible)value).ToType(destinationType, culture);
}
}
Now on the MaxLongNumericValue
property you use an attribute like this:-
[TypeConverter(typeof(ConvertibleTypeConverter<long>))]
public long MaxLongNumericValue { get; set; }
Now when the Xaml parser gets to this property it will defer to the specified TypeConverter
.