【问题标题】:How to bind contained objects with live binding如何使用实时绑定绑定包含的对象
【发布时间】:2014-02-03 13:23:00
【问题描述】:

Malcolm Groves 有一篇关于 TAdapterBindSource 和绑定到对象的博客。 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;

问题是如何将数据(联系人)加载到第二个 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;

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

【问题讨论】:

  • 快速浏览一下 XE5 源代码,我会说 AdapterBindSourceContact.SetList(AContacts, False) 应该可以胜任。
  • @StefanGlienke: AdapterBindSourceContact 是一个 TPrototypeBindSource 并且没有过程 SetList,TAdapterBindSource 也没有 setList
  • 有这个方法的是BindSourceAdapter。 TPrototypeBindSource 有一个受保护的方法 GetInternalAdapter,您可以在其中获取它。但我对 LB 架构不太熟悉,因为我觉得它是 BS。
  • 是的,有一个 SetList,但我收到错误 TGeneratorRecord 和 TContract 类型不兼容。 BindSourceAdapter...你主要的TAdapterBindSource?
  • 一头公牛的粪便 :)

标签: delphi delphi-xe5 livebindings


【解决方案1】:

使用您的 ListBindSourceAdapter 投射 internalAdapter 即可完成这项工作。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多