views:

306

answers:

1

Hi,

What are the best practices or tips with respect to data binding to objects?

I have a complex data binding scenario where my winform has a BindingSource set to an object. This object is a class of type Customer and the forms are bound the customer's properties like "firstName", "lastName", etc.

Each property has the following pattern (pseudo-code)

  • get: return mProperty
  • set: mProperty = value; PropertyHasChanged()

Some changes to certain properties requires automatically setting other values. For example:

  • Choosing a country will load a second dropdown with a list of states/provinces
  • Checking a checkbox (databound to a boolean property) will set the country and state/province to specific values

Where do I put the code that will set the country and state/province based on the checkbox? If I set the properties in the wrong order, (province before the country) the change to the country will force a change of the province list and override the selected province since it won't be in the list (this is a trivial example, but reflects the timing issues).

What are the best practices or tips with respect to data binding to objects?

A: 

Binding doesn't have to be one to one. You can listen on a Country or Checkbox change to determine the province for example.

What you have though looks like a state flow, in a given state, do X. Add a 'hidden' property that correctly represents the state you want to use for your bindings: PreDefined, Normal, Advanced and then the binding can also act on this as a 'tip' to know how to properly display itself.

I hope this helps.