Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

XE3 Visual LiveBindings: User defined objects

Author: Jim T1392

The adapter components TListBindSourceAdapter and TObjectBindSourceAdapter enable LiveBindings with user defined TObjects.

Here is a user defined TObject  for example.

[crayon-6768557a62e0d786942201/]

[crayon-6768557a62e14894951476/]

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:

[crayon-6768557a62e18901879032/]

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:

[crayon-6768557a62e1d627000548/]

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.

[crayon-6768557a62e20887076632/]

A complete implementation of a custom adapter publishes properties and methods:

[crayon-6768557a62e24507725411/]

TObjectBindSourceAdapter is used instead of TListBindSourceAdapter to bind to a single object, rather than a list of objects.

TObjectBindSourceAdapter is instantiated like this:

[crayon-6768557a62e26815550432/]

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

Exit mobile version