Wednesday, January 04, 2006 12:24 AM
Lately, I've been trying to work more with databinding in my projects. I'm accustomed to thinking of it as a means of associating user interface controls with a database, but in fact it's much more than that! The fact is, you can databind to a variety of types of data, without any need for DataSets. The coolest thing I've come across recently has been databinding to application settings. Settings are new to Visual Studio 2005 and allow you to define user and application settings that are managed by the application framework. You name a property like “AvatarFilename,” set it a datatype like String, optionally provide a default value, and you're good to go. You get a strongly-typed object to reference the settings just as object properties, you can reload and save the properties with a single method call, and the plumbing is completely taken care of for you. The data is actually binary serialized to the machine or user profile folder (Documents and Settings in XP), and all work required to find, read, and write the file is done for you.
My most recent discovery was that each user interface control can be bound to a specific setting with a few clicks in the Properties window. You can select an already-created setting, or add a new one from that interface. At runtime, the settings will be loaded, the fields will be populated, and any changed to the control contents will automatically be propagated back to the settings. To keep the power in your hands, you still have the ability to save or reload the settings at will. It's automagic, and fun too! ;-)
An upcoming In the Box column of mine will discuss this is a little further with a screenshot, but I may document it a bit more fully in a future blog entry. Databinding is one of those great things about .NET that you glimpse a little bit at a time, and really cut down on your code while expanding the available feature set.