Can BindingManagerBase or inheriting classes handle updating object controls are Data Bound to?
The project I came into manages databinding like this:
- Default object is loaded on form load.
- Object is databound to each control property by property in code.
- User selects a different object to view.
- All controls have their d开发者_JAVA技巧atabindings cleared.
- All controls have a databinding added referencing the new object instead of the old.
I have seen some places in the code where previous developers used BindingManagerBase, but was unsure of the reason for it.
During my research it seems it is used to simplify databinding in the manner of PropertyManager but I can't figure out how to update it's "Current" property. Can it reduce the steps above to the following?
- Default object is loaded.
- Databindings added, and BindManager retrieved.
- User selects a different object to view.
- BindManager's reference of databount object is updated, propogating to all controls.
If not, is there an class capable of doing this?
Also is there any way that this can be done automatically with some property on the controls themselves.
Simply instantiate some hypothetical binding manager that looks for controls with a property set, and databinds them to an object specified, thus removing the need to programmatically databind each control to begin with?
If this is Windows Forms, then I would recommend using a BindingSource - then you only have to set the databindings once and update the BindingSource's DataSource property when you load a new object.
(BindingManagerBase is something else - it's for when you have two controls bound to the same data source.)
Your steps would be:
- Create BindingSource
- Assign an empty object
- Create DataBindings between the controls to the BindingSource
- Assign objects to the BindingSource's DataSource property as required.
精彩评论