Binding.OnFormat fails if your control value is of type "Object"
When using simple databinding, you can hook up your own code to OnFormat and OnParse events, thus supporting displaying of data in user friendly way. For example, you can make $ amounts display as $1.23, but save to the object/data store as “123″, or “1.23USD” or whatever.
I know this works, because I have used it in the past. But for most of the morning I have been trying to determine why my OnFormat event was not firing. OnParse fired fine, with the result that my data displayed in it’s “ugly” form (e.g. “123″), but OnFormat simply never happened. Very annoying.
Eventually I was reduced to using .NET Reflector to review the .NET framework Binding code. What I discovered was this: When binding to a control property of type “Object”, the format event will never fire. Never. Aaaaargggh! Relevant .NET framework code below:
Private Function FormatObject(ByVal value As Object) As Object If Me.ControlAtDesignTime Then Return value End If Dim type1 As Type = Me.propInfo.PropertyType If (type1 Is GetType(Object)) Then Return value End If Dim args1 As New ConvertEventArgs(value, type1) Me.OnFormat(args1)
...End Function