This page describes a problem with .NET simple data binding – a property of a custom type couldn’t be bound correctly to the
Text property of a standard TextEdit, because the internal logic would never convert the string value back to the object type. A given
TypeConverter would simply be ignored in the process. The only workaround was to use the
Parse event of the
Binding object – not a nice way to go because the code wouldn’t be central to the class and its type converter any more.
While looking at the reasons behind this, I found a workaround for this in .NET 2: the
FormattingEnabled property of the
Binding object. While this is documented to do something quite different, the changed logic of the
Binding.OnParse method actually checks for this parameter and the problem doesn’t show when
FormattingEnabled is set to true for the binding. It’s possible to pass
true for this parameter right when creating the binding, like this:
textBox1.DataBindings.Add("Text", myObject, "MyProperty", true);
Now I’m still wondering if I’m missing something here… the original problem, although it seems to be as described on the NoiseEHC page, sounds so… stupid? And the small change in .NET 2, while it does the trick, doesn’t look like a purposeful fix of the issue to me. Hm.
Sorry, this blog does not support comments. Why not?
I used various blog hosting services since this blog was established in 2005, but unfortunately they turned out to be unreliable in the long term and comment threads were lost in unavoidable transitions. At this time I don't want to enable third-party services for comments since it has become obvious in recent years that these providers invariably monetize information about their visitors and users.
Please use the links in the page footer to get in touch with me. I'm available for conversations on Keybase, Matrix, Mastodon or Twitter, as well as via email.