Ravaut123
Ravaut123

Reputation: 2808

How to bind contained objects with live binding

There is a blog about TAdapterBindSource and binding to objects by Malcolm Groves. http://www.malcolmgroves.com/blog/?p=1084

This works fine but how can I bind a contained object from a class.

TContact = class
private
  FPhoneNumber: String;
public
  property PhoneNumber : String read FPhoneNumber write FPhoneNumber;  
end;

TPerson = class
private
  FAge: Integer;
  FLastname: string;
  FFirstname: string;
  FContacts:  TObjectList;
public
  constructor Create(const Firstname, Lastname : string; Age : Integer); virtual;
  property Firstname : string read FFirstname write FFirstname;
  property Lastname : string read FLastname write FLastname;
  property Age : Integer read FAge write FAge;
  property Contacts:  TObjectList<TContact> read FContacts write FContacts;
end;

On the form I have private TObjectList

MyPeople : TObjectList<TPerson>;

I also have two TPrototypeBindSource. One for TPerson and one for TContact

procedure TForm1.AdapterBindSourcePersonCreateAdapter(Sender: TObject;
var ABindSourceAdapter: TBindSourceAdapter);
begin
  MyPeople := TObjectList<TPerson>.Create;

  MyPeople.Add(TPerson.Create('Fred', 'Flintstone', 40));
  MyPeople.Add(TPerson.Create('Wilma', 'Flintstone', 41));
  MyPeople.Add(TPerson.Create('Barney', 'Rubble', 40));
  MyPeople.Add(TPerson.Create('Betty', 'Rubble', 39));

  ABindSourceAdapter := TListBindSourceAdapter<TPerson>.Create(self, MyPeople, True);
end;

procedure TForm1.AdapterBindSourceContactCreateAdapter(Sender: TObject;
var ABindSourceAdapter: TBindSourceAdapter);
begin
  ABindSourceAdapter := TListBindSourceAdapter<TContact>.Create(self);
end;

procedure TForm1.lstPersonsItemClick(const Sender: TObject; const AItem: TListViewItem);
var
  AContacts:  TObjectList <TContact>;
begin
  AContacts:=  TPerson(MyPeople.Items[1]).Contacts;

  //self.AdapterBindSourceContact.???  --> How to insert list
  //self.AdapterBindSourceContact.DataGenerator.SetList(AContacts, True);  --> doesn't work
end;

The problem is how can I load the data (Contacts) into the second TPrototypeBindSource.

One day later... Once upon a time you get a vision. I've change AdapterBindSourceContact (TPrototypeBindSource) into TAdapterBindSource

procedure TForm1.lstPersonsItemClick(const Sender: TObject; const AItem: TListViewItem);
var
  AContacts:  TObjectList <TContact>;
  ABindSourceAdapter: TBindSourceAdapter;
begin
  AContacts:=  TPerson(MyPeople.Items[1]).Contacts;
  ABindSourceAdapter := TListBindSourceAdapter<TContract>.Create(self, AContacts);
  self.AdapterBindSourceContact.Adapter:= ABindSourceAdapter;
  self.AdapterBindSourceContact.Active:= True;
end;

But I don't know if this is the right way of working.

Upvotes: 1

Views: 3645

Answers (1)

Robson Coutinho
Robson Coutinho

Reputation: 131

Cast the internalAdapter with your ListBindSourceAdapter will do the job.

with TListBindSourceAdapter<TContact>(AdapterBindSourceContact.internalAdapter) do
begin
   SetList(AContacts, False);
   Active := true;
end;

Upvotes: 3

Related Questions