I am trying to use livebindings on a VCL form in which the object to be bound to the controls on the form is passed to the form as a property. I am using 10.1 Berlin. The property where in the object is passed is ordinary:
Public
Property ProjectObject: TProject Read fProjectObject Write fProjectObject;
I have used DataGeneratiorAdapter and AdapterBindSource to set up the links on the form using the designer.
Where I am having a lack of understanding is at the AdapterBindSource in the OnCreateAdapter method. All the examples I can find show how to create a new object to be populated by the controls, but I fail to find a way to bind at runtinme fProjectObject (the passed object).
My current code in the OnCreateAdapter method is:
ABindSourceAdapter := TObjectBindSourceAdapter<TProject>.Create(Self);
Which is acceptable to the compiler, but does not allow the controls to display and update the properties in fProjectObject.
The one of the sections of code that displays this form (the project edit form) looks like this:
ProjEdit.ProjectObject := Proj;
ProjEdit.ShowModal;
StoreProject(Proj);
Where ProjEdit is the project edit form, ProjectObject is the property where the project object is passed and Proj is the project object to be edited. The project object is simply passed to this form and stored after any alterations to the information have been made. This object was stored in a database before being passed to this form for editing.
How do I connect the livebindings to the passed object?
Thanks in advance for your help
Here's what I suggest:
First: In the CreateAdapter of the AdapterBindSource use the following:
procedure TfrmProjectEdit.AdapterBindSource1CreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter);
begin
fProjectObject:=TProject.Create;
ABindSourceAdapter:=TObjectBindSourceAdapter<TProject>.Create(self, fProjectObject, True);
end;
Second: Use a setter for the project property such as:
procedure TfrmProjectEdit.SetProject (aProject: TProject);
begin
fProjectObject:=aProject;
AdapterBindSource1.Refresh;
end;
Quick explanation: The AdapterBindSource will own the fProjectObject and release it when the ABS is released. We simply assign a new value to the fProjectObject and Refresh the ABS in the setter.
I have not tested out this code - but I think this should work...