如何使用实时绑定绑定包含的对象

时间:2014-02-03 13:23:01

标签: delphi delphi-xe5 livebindings

有一篇关于TAdapterBindSource和Malcolm Groves绑定到对象的博客。 http://www.malcolmgroves.com/blog/?p=1084

这很好但是如何从类中绑定一个包含的对象。

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;

在表单上我有私有TObjectList

MyPeople : TObjectList<TPerson>;

我还有两个TPrototypeBindSource。 一个用于TPerson,一个用于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;

问题是如何将数据(Contacts)加载到第二个TPrototypeBindSource中。

一天后...... 曾几何时你会有一个愿景。 我已将AdapterBindSourceContact(TPrototypeBindSource)更改为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;

但我不知道这是否是正确的工作方式。

1 个答案:

答案 0 :(得分:2)

使用ListBindSourceAdapter构建internalAdapter将完成这项工作。

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