Delphi XE2 之 FireMonkey 入门(27) - 数据绑定: TBindingsList: TBindScope
如果在编写表达式时, 如果能够随意指认需要的控件就好了(通过 Owner 也可以勉强做到), TBindScope 就是解决这个问题的.
示例设想: 把三个 TEdit 的 Text 绑定到一个 TLabel.
在窗体上添加 Label1、Edit1、Edit2、Edit3、BindingsList1、BindScope1; 激活 Edit1 和窗体的默认事件.
unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, Data.Bind.EngExt, Fmx.Bind.DBEngExt, Data.Bind.Components, FMX.Layouts, FMX.Edit; type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Label1: TLabel; BindingsList1: TBindingsList; BindScope1: TBindScope; procedure FormCreate(Sender: TObject); procedure Edit1Change(Sender: TObject); end; var Form1: TForm1; implementation {$R *.fmx} procedure TForm1.FormCreate(Sender: TObject); begin with TBindExpression.Create(BindingsList1) do begin ControlComponent := Label1; ControlExpression := 'Text'; SourceComponent := BindScope1; //把 BindScope1 指定为源组件, 之后可以在表达式中直接使用控件名 SourceExpression := 'Format("%s,%s,%s", Edit1.Text, Edit2.Text, Edit3.Text)'; Active := True; end; BindScope1.Active := True; // Edit2.OnChange := Edit1.OnChange; Edit3.OnChange := Edit1.OnChange; end; procedure TForm1.Edit1Change(Sender: TObject); begin BindingsList1.Notify(Sender, ''); end; end.