XE3 Visual LiveBindings: User defined C++ objects

Posted by on in Blogs
In the previous post, TListBindSourceAdapter<TContact> is used to enable a list of Delphi TContact objects with LiveBindings, using the CreateAdapter event handler:
implementation

{$R *.dfm}

uses ContactsUnit, System.Generics.Collections;

procedure TForm1.AdapterBindSource1CreateAdapter(Sender: TObject;
var ABindSourceAdapter: TBindSourceAdapter);
begin
ABindSourceAdapter := TListBindSourceAdapter<TContact>.Create(Self,
LoadContacts);
end;

C++ custom object can also be enabled for LiveBindings.   Here is a C++ version of TContact for example:
#pragma explicit_rtti methods (__published, public) properties (__published, public) fields(__published, public)
class TContact: public TObject
{
private:
System::String firstname;
System::String lastname;
public:
__property System::String FirstName = {read=firstname, write=firstname};
__property System::String LastName = {read=lastname, write=lastname};

TContact()
{
firstname = "<first>";
lastname = "<last>";
}

TContact(const System::String first, const System::String last)
{
firstname = first;
lastname = last;
}

};

The implementation of the CreateAdapter event is a little different than Delphi.  C++ can't create a new generic type like TListBindSourceAdapter<TContact>.  The non-generic version of TListBindSourceAdapter is used instead.
void __fastcall TForm1::AdapterBindSource1CreateAdapter(TObject *Sender, TBindSourceAdapter *&ABindSourceAdapter)

{
ABindSourceAdapter = new TListBindSourceAdapter(this,
LoadContacts(), __classid(TContact), true);

}

Note that the class type is passed as a parameter.

A C++ sample project is available on source forge:
https://radstudiodemos.svn.sourceforge.net/svnroot/radstudiodemos/branches/RadStudio_XE3/LiveBindings/observers

The project is called CppListAdapterEventProject.dpr


Comments

Check out more tips and tricks in this development video: