Groups | Blog | Home
all groups > dotnet windows forms databinding > november 2006 >

dotnet windows forms databinding : custom TypeConverter.ConvertFrom not being called


Joanna Carter [TeamB]
11/10/2006 12:00:00 AM
I have designed a custom type :

public abstract class Numeric
{
private decimal value;

public Numeric(decimal value)
{
this.value = value;
}

public decimal Value
{
get { return value; }
}

public virtual uint Size
{
get { return 5; }
}

public virtual uint Precision
{
get { return 2; }
}

public override string ToString()
{
return value.ToString();
}
}

[TypeConverter(typeof(NumericTypeConverter<Quantity>))]
public class Quantity : Numeric
{
public static implicit operator Quantity(decimal input)
{
return new Quantity(input);
}

public static implicit operator decimal(Quantity input)
{
return input.Value;
}

public Quantity(decimal value) : base(value) { }

public override uint Precision
{
get { return 0; }
}
}

I then went on to create a TypeConverter :

public class NumericTypeConverter<typeT> : TypeConverter where typeT :
Numeric
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type
sourceType)
{
return sourceType == typeof(string) ? true :
base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
// use LCG to call constructor of typeT with value as parameter
}

return base.ConvertFrom(context, culture, value);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type
destinationType)
{
return destinationType == typeof(string) ? true :
base.CanConvertTo(context, destinationType);
}

public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
// use LCG to call constructor of typeT with value as parameter
}

return base.ConvertFrom(context, culture, value);
}

public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type
destinationType)
{
if (destinationType == typeof(string))
return (value as INumericType).Value.ToString();

return base.ConvertTo(context, culture, value, destinationType);
}
}

On a form, I create an instance of a class with a Quantity property, and add
a Binding to a TextBox control :

public class Test
{
private Quantity price;

public Quantity Price
{
get { return price; }
set { price = value; }
}
}

{
Test test = new Test();

test.Price = 12.34M;

Binding b = new Binding("Text", test, "Price");

textBox1.DataBindings.Add(b);
}

When I edit the value from its original 12.34 and then exit the TextBox, the
ConvertTo method of the type converter is called and the value in the
TextBox reverts to 12.34 !!!

Why is it that the CanConvertTo and ConvertTo methods of the type converter
get called, but not the CanConvertFrom and ConvertFrom methods don't ??

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Joanna Carter [TeamB]
11/10/2006 11:58:25 AM
"Bart Mermuys" <bmermuys.nospam@hotmail.com> a écrit dans le message de
news: VZZ4h.176884$mw2.2715739@phobos.telenet-ops.be...

| It seems to work when you enable formatting :
| Binding b = new Binding("Text", test, "Price", true);

Thank you, thank you, thank you !!!

Two of us have been scratching our heads for about 4 hours about this one,
getting all sorts of bad advice from Google searches. Now we can get on with
the rest of the days work :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Bart Mermuys
11/10/2006 12:09:25 PM
Hi,

[quoted text, click to view]

It seems to work when you enable formatting :
Binding b = new Binding("Text", test, "Price", true);

Not sure why, though it seems that when FormattingEnabled is false, it only
checks for a Source (TextBox.Text) converter converter which doesn't work,
eg.:
TypeDescriptor.GetConverter(typeof(string)).CanConvertTo( Quantity );

If FormattingEnabled is true it does check for a Target (Test.Price)
converter and uses CanConvertFrom.

HTH,
Greetings


[quoted text, click to view]

AddThis Social Bookmark Button