Author: Jim T1392
The adapter components TListBindSourceAdapter and TObjectBindSourceAdapter enable LiveBindings with user defined TObjects.
Here is a user defined TObject for example.
One way to use TListBindSourceAdapter is to instantiate the adapter at run time. Drop a TAdapterBindSourceAdapter on the form and implement the CreateAdapter event as follows:
In the LiveBindings designer, we won’t see the fields for TContact because the TContact type is not known at design time.
Drop a TStringGrid on the form and connection the ‘*’ of AdapterBindSource1 to StringGrid1.
A navigator can be added by right clicking AdapterBindSource1 and choosing “Add Navigator”.
The grid is populated when the application is run:
A TDataGeneratorAdapter can be used to provide some fields to use at design time. Add fields with the same name as the properties in TContact. Set AdapterBindSource1.Adapter to DataGeneratorAdapter1.
The form shows sample data and the LiveBindings designer shows field names.
To show this data at design time, the TDataGeneratorAdapter fields have FieldType: ftString and Generator: LoremIpsum.
The LiveBindings designer can be used to connect fields to controls.
Note that an easy way to create a TLabeledEdit is to right click on FirstName then choose “Link to new control…”.
At runtime, the TListBindSourceAdapter will be used instead of DataGeneratorAdapter1 because of the CreateAdapter event.
Another way to use TListBindSourceAdapter is to register a custom component. Instead of instantiating a TListBindSourceAdapter in the CreateAdapter event, the custom component is dropped from the tool palette. A TDataGeneratorAdapter is not needed.
The minimal code to for a custom adapter looks like this:
To register the custom adapter, you will need to create and register a package which contains the custom adapter component and TContact class. You will also need and a Register method to register the custom component.
A complete implementation of a custom adapter publishes properties and methods:
TObjectBindSourceAdapter is used instead of TListBindSourceAdapter to bind to a single object, rather than a list of objects.
TObjectBindSourceAdapter is instantiated like this:
At run time, there is only a single row of data.
Source forge samples
https://radstudiodemos.svn.sourceforge.net/svnroot/radstudiodemos/branches/RadStudio_XE3/LiveBindings/observers
- ContactsAdapterPackage.dpk
- Implements a custom TListBindSourceAdapter component
- ListAdapterEventProject.dpr
- Demonstrates TAdapterBindSource.CreateAdapter event
- ListAdapterComponentProject.dpr
- Demonstrates custom TListBindSourceAdapter component
- Project: CppListAdapterEventProject.dpr
- Cpp project demonstrates TAdapterBindSource.CreateAdapter event