Delphi LiveBindings如何绑定一个对象(转)
原文 http://www.malcolmgroves.com/blog/?p=1084
一、新建VCL工程加入TAdapterBingSource控件
二、定一个TPerson类
MyPerson : TPerson;
TPerson = class private FAge: Integer; FLastname: string; FFirstname: string; 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; end;
三、双击并加入事件代码
1.创建一个对象实列Person
2.创建一个BindSourceAdapter
- self, 指定TObjectBindSource的归属
- MyPerson,需要邦定对象实例
- True,是否自动释放,如果否则False 默认值为True
procedure TForm4.AdapterBindSource1CreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter); begin MyPerson :=TPerson.Create('test','test1', 13); ABindSourceAdapter := TObjectBindSourceAdapter<TPerson>.Create(Self, MyPerson, True); end;
四、放一个TStringGrid对象 在Events LiveBindings点击BindVisually
点击运行