I have my own type C # type (just an example):
public class MyVector { public double X {get; set;} public double Y {get; set;} public double Z {get; set;}
And I want it to bind to TextBox.Text:
TextBox textBox; public MyVector MyVectorProperty { get; set;}
Essentially, I need to convert to and from a string for my custom value type. In the text box, I need something like "x, y, z" that you can edit to update the vector type. I suggested that I can do this by adding the TypeConverter class:
public class MyVectorConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) return true; //... return base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { if (destinationType == typeof(string)) return true; //... return base.CanConvertTo(context, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string) { MyVector MyVector; //Parse MyVector from value return MyVector; } return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { string s; //serialize value to string s return s; } //... return base.ConvertTo(context, culture, value, destinationType); } }
and linking it to my structure:
[TypeConverter(typeof(MyVectorConverter))] public class MyVector {
This seems to end half the battle. I see that MyVectorConverter getting a call, but something is wrong. It is called to find out if it knows how to convert to a string, then it is called to convert to a string. However, it is never asked if it can convert the FROM string or actually perform the conversion. In addition, immediately after editing in the text field, the old value is immediately replaced (another sequence of CanConvertTo and ConvertTo, restoring the old value). The end result is that the newly entered entry in the text field is returned immediately after its application.
It seems to me that there is simply something simple. Here? Is this whole project / approach doomed to failure? Is anyone else trying such madness? How to bidirectionally associate a custom, multi-page type with string control?
Solution: In particular, all that is needed is that βformattingβ must be enabled on the Binding object. (thanks, John Skeet):
textBox.DataBindings.Add("Text", this, "MyVectorProperty"); //FAILS textBox.DataBindings.Add("Text", this, "MyVectorProperty", true); //WORKS!
Oddly enough, everything that my MSDN mentions about this parameter (formattingEnabled):
"true to format the displayed data, otherwise false
It does not say that it is a requirement to return data from control (in these conditions).