Binding.FormattingEnabled fixes two-way simple binding

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.

1 Comment on Binding.FormattingEnabled fixes two-way simple binding

  1. It seems that they made a coding mistake and then later have kept that way for compatibility purposes…

    Like

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s