About a week ago I described how you can use a command to format a phone number in a textbox.
I’d like to offer you a new way of formatting the same phone number, in the text box.
This time around we’ll be binding the IPhoneNumber type directly to the text box.
When changes are made to the text in the text box a new phone number type will be constructed and bound to the text box.
Without further ado I would like to present to you the BindableTextBox
This is a generic type that will bind the text box control text property to the value returned by the type ‘T’s “ToString()” override. The “CreateType
When it’s time to flush data from the view back to the domain, you can inspect the BindableTextBox’s “Value” property which will hand you the current value contained in the type. In this example it would hand you the current “IPhoneNumber”. This may work well for simple types, but maybe not so much on more complex types. One thing you could do is change the input parameter of the CreateType delegate to take in a “IBindableTextBox” instead of a control, thus giving you access to the current “Value” that is bound to the text box, in case you need to copy over additional information to the new type.
Either way to use this type it’s about as simple as this:
As usual the full source is provided… PhoneNumberView2.cs.txt (1.7 KB)