2014-02-03 1 views
1

Существует блог о 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; 

Проблема заключается в том, как я могу загрузить данные (контакты) во второй 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; 

Но я не знаю, если это правильный способ работы.

+0

Из беглого взгляда на источники xe5 я бы сказал 'AdapterBindSourceContact.SetList (AContacts, False)' должен делать эту работу. –

+0

@StefanGlienke: AdapterBindSourceContact - это TPrototypeBindSource и не имеет процедуры SetList, также TAdapterBindSource no setList – Ravaut123

+0

Это BindSourceAdapter, который имеет этот метод. TPrototypeBindSource имеет защищенный метод GetInternalAdapter, где вы можете его получить. Но я не слишком знаком с архитектурой LB, потому что считаю BS. –

ответ

2

Вставьте внутренний адаптер с помощью своего адаптера ListBindSourceAdapter.

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

 Смежные вопросы

  • Нет связанных вопросов^_^