TypeConverter at property level

617 views
Skip to first unread message

Kent Boogaart

unread,
Apr 23, 2009, 2:34:28 PM4/23/09
to WPF Disciples
Anyone here actually managed to get a TypeConverter applied at the
property level to work? Seems to me WPF just ignores it unless it's
applied at the class level. According to MSDN it should work (see last
section at http://msdn.microsoft.com/en-us/library/aa970913.aspx) but
according to my simple test, it does not. And that's regardless of
whether it's a DependencyProperty or regular CLR property.

Is this a known bug? Am I doing something stupid?

Best,
Kent

ken...@internode.on.net

unread,
Apr 23, 2009, 2:42:56 PM4/23/09
to WPF Disciples
Here's a little "hackery" I just came up with to get WPF to use the
converter, but this really shouldn't be necessary.

Best,
Kent


public partial class Window1 : Window
{
public static readonly DependencyProperty NumberProperty =
DependencyProperty.Register("Number",
typeof(Number),
typeof(Window1));

static Window1()
{
TypeDescriptor.AddProvider(new NumberDescriptionProvider(), typeof
(Number));
}

[TypeConverter(typeof(NumberConverter))]
public Number Number
{
get { return GetValue(NumberProperty) as Number; }
set { SetValue(NumberProperty, value); }
}

public Window1()
{
InitializeComponent();

Number = new Number() { Value = 26 };
DataContext = this;
}
}

public class Number
{
public int Value
{
get;
set;
}
}

public class NumberConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{
return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext context,
Type destinationType)
{
return base.CanConvertTo(context, destinationType);
}
}

public class NumberDescriptionProvider : TypeDescriptionProvider
{
public override ICustomTypeDescriptor GetTypeDescriptor(Type
objectType, object instance)
{
return new NumberTypeDescriptor();
}
}

public class NumberTypeDescriptor : CustomTypeDescriptor
{
public override TypeConverter GetConverter()
{
return new NumberConverter();
}
}

Colin Eberhardt

unread,
Apr 23, 2009, 2:59:48 PM4/23/09
to WPF Disciples
Hi Kent,

If it's of any help to you, the Silverlight Toolkit uses
TypeConverters for CLR properties which wrap DPs, look at the Minimum
property on DateTimeAxis for example:

http://silverlight.codeplex.com/SourceControl/changeset/view/18791#507608

The toolkit builds for WPF also, so it should work for you!

Hope that helps.

Regards,
Colin E.



On Apr 23, 7:34 pm, Kent Boogaart <ken...@internode.on.net> wrote:
> Anyone here actually managed to get a TypeConverter applied at the
> property level to work? Seems to me WPF just ignores it unless it's
> applied at the class level. According to MSDN it should work (see last
> section athttp://msdn.microsoft.com/en-us/library/aa970913.aspx) but

ken...@internode.on.net

unread,
Apr 23, 2009, 3:07:16 PM4/23/09
to WPF Disciples
Thanks Colin. That's basically what I tried (ie. the code I posted
minus the call to TypeDescriptor.AddProvider). It builds fine, but the
TypeConverter isn't used unless declared against the class, or unless
I jump through hoops with the custom TypeDescriptor. Perhaps this
works in SL, but not WPF...

Best,
Kent

On Apr 23, 7:59 pm, Colin Eberhardt <colin.eberha...@gmail.com>
wrote:
> Hi Kent,
>
> If it's of any help to you, the Silverlight Toolkit uses
> TypeConverters for CLR properties which wrap DPs, look at the Minimum
> property on DateTimeAxis for example:
>
> http://silverlight.codeplex.com/SourceControl/changeset/view/18791#50...

Mike Brown

unread,
Apr 23, 2009, 3:46:10 PM4/23/09
to wpf-di...@googlegroups.com
Kent,
   Your TypeConverterAttribute should be applied to the Number class, instead of the property. When you registered the NumberDescriptionProvider (which will be applied globally) in your static constructer. It informed the binding system to look to it for extended type description. I'm assuming that your NumberDescriptor returns a NumberConverter from it's GetConverter method. That's why it's working properly now. Simple approach is to put the TypeConverterAttribute on the Number class itself.
 
If that's not possible and you only want the NumberConverter to be applied to the return from your property use the following
 
               public Number Number
               {
                       get
                       {
                         var retVal = (Number)GetValue(NumberProperty);
                         TypeDescriptor.AddProvider(new NumberDescriptionProvider, retVal);
                         return retVal;
                       }
                       set { SetValue(NumberProperty, value); }
               }
 
HTH.

Mike Brown

unread,
Apr 23, 2009, 3:48:52 PM4/23/09
to wpf-di...@googlegroups.com
It looks like silverlight's binding goes above and beyond WPF's by checking for the TypeConverter attribute on properties.

Mike Brown

unread,
Apr 23, 2009, 3:56:37 PM4/23/09
to wpf-di...@googlegroups.com
I never commented on this before but in WPF 1.0 (not sure if it's been fixed since then) the docs said that the binding system respects ICustomTypeDescriptors but it didn't appear to work for me. I worked around it with the dynamic dependency property utility, but would have preferred not to have had to write that.

ken...@internode.on.net

unread,
Apr 23, 2009, 4:04:35 PM4/23/09
to WPF Disciples
Thanks Mike. The code I posted works by virtue of the call to
TypeDescriptor.AddProvider(). Remove that, and it doesn't. The point
is to determine why this doesn't work at the property level. I
understand I can apply the attribute to the class in this case, but in
cases where you don't own the type being converted, you need the wacky
workaround.

So this is either a bug in WPF, or a bug in documentation. I'd vote
for bug in WPF since it works elsewhere.

Best,
Kent

On Apr 23, 8:56 pm, Mike Brown <mbrow...@gmail.com> wrote:
> I never commented on this before but in WPF 1.0 (not sure if it's been fixed
> since then) the docs said that the binding system respects
> ICustomTypeDescriptors but it didn't appear to work for me. I worked around
> it with the dynamic dependency property utility, but would have preferred
> not to have had to write that.
>
> On Thu, Apr 23, 2009 at 3:48 PM, Mike Brown <mbrow...@gmail.com> wrote:
> > It looks like silverlight's binding goes above and beyond WPF's by checking
> > for the TypeConverter attribute on properties.
>

Mike Brown

unread,
Apr 23, 2009, 4:29:36 PM4/23/09
to wpf-di...@googlegroups.com
Digging through with reflector right now because I'm bored. Looks like the Binding gets turned into an expression with a typeconverter of expression converter. I'm guessing the issue lies somewhere in there.

Mike Brown

unread,
Apr 23, 2009, 4:34:21 PM4/23/09
to wpf-di...@googlegroups.com
Dead end there...Expression converter returns false or throws an exception on everything.

Mike Brown

unread,
Apr 23, 2009, 5:24:00 PM4/23/09
to wpf-di...@googlegroups.com
Had the bright idea...why not just use the debugger since we have the source symbols now. No dice...the reference source server is severely outdated with very little response from MS in the source browser. Still curious why the framework doesn't ship with source like they used to do with the Visual C++ Runtime.
Reply all
Reply to author
Forward
0 new messages