In my second blog post on Using LiveBindings in a VCL application, I looked at using the Prototype bind source for the first time and how it can be used to link objects to screens. But what if you need to work this out dynamically?
Dynamically linking to objects is possible if you create the LiveBindings at run time and in this video I show how to easily work out the components and properties you need to setup. This post builds on what was learned in the previous posts VCL LiveBindings and DataSets and VCL LiveBindings to Objects
While live bindings are easy to build at design time, sometimes its nice to see them in code. Building them manually is a case of linking the values together using the same objects created at design time.
Linking to List
The LinkListControlToField allows you to link a specific field into a List. The example in the video looks at using the TListView showing the list of TFoo objects in the list represented by their Name property.
ListLink :=TLinkListControlToField.Create(Self); ListLink.DataSource := PrototypeBindSource1; ListLink.FieldName := 'Name'; ListLink.Control := ListView1;
Linking to Control
The TLinkControlToField allows you to link a specific property to a control. It also automatically helps control data input for controls (e.g. it makes TEdit only accept Integers for Integer fields).
LinkControl := TLinkControlToField.Create(Self); LinkControl.DataSource := PrototypeBindSource1; LinkControl.FieldName := 'Value'; LinkControl.Control := Edit1; LinkControl.Track := True;
The Track option means that notifications are sent more frequently. Imaging a run-time example using a TCheckBox that is bound to a field in a database table:
- If Track is set for the checkbox, then the change occurs immediately when you click the checkbox.
- If Track is not set, the change does not occur until the checkbox control loses focus.
If you do you LiveBindings are runtime, then you may need to create tell the DataSource to refresh. This is easily done by setting setting the PrototTypeBindSource.Active property to False and back to True.