如果要给一对 "源控件" 和 "目标控件" 写多个表达式, 使用 TBindExpression 就不如 TBindExprItems 了.
TBindExprItems 中的表达式又分两组: FormatExpressions、ClearExpressions, 后者是在断开绑定时的表达式.
示例设想:
1、TrackBar1 为源, 把其 Value 值绑定给 Edit1.Text 和 Edit1.Width 和 Label1.Text;
2、断开绑定时, 在 Edit1 中显示 "已断开绑定".
先在窗体上添加: TrackBar1、Edit1、Label1、BindingsList1、CheckBox1, 然后激活 TrackBar1、ChengkBox1 和窗体的默认事件:
unit Unit1;interfaceusesSystem.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.Edit;typeTForm1 = class(TForm)TrackBar1: TTrackBar;Edit1: TEdit;Label1: TLabel;BindingsList1: TBindingsList;CheckBox1: TCheckBox;procedure FormCreate(Sender: TObject);procedure TrackBar1Change(Sender: TObject);procedure CheckBox1Change(Sender: TObject);end;varForm1: TForm1;implementation{$R *.fmx}varbindExprItems: TBindExprItems;procedure TForm1.FormCreate(Sender: TObject); beginbindExprItems := TBindExprItems.Create(BindingsList1);with bindExprItems dobeginControlComponent := Edit1; //目标控件SourceComponent := TrackBar1; //源控件{把 TrackBar1.Value 绑定到 Edit1.Text}with FormatExpressions.AddExpression dobeginControlExpression := 'Text';SourceExpression := 'Value';end;{把 TrackBar1.Value 绑定到 Edit1.Width}with FormatExpressions.AddExpression dobeginControlExpression := 'Width';SourceExpression := 'Value';end;{把 TrackBar1.Value 绑定到 Label1.Text}with FormatExpressions.AddExpression dobeginControlExpression := 'Owner.Label1.Text'; //OwnerSourceExpression := 'Value';end;{当要断开绑定时...}with ClearExpressions.AddExpression dobeginControlExpression := 'Self.Text'; //这里的 Self 是指目标控件本身, 可省略SourceExpression := '"已断开绑定"';end;Active := True;end;CheckBox1.IsChecked := True;TrackBar1.Max := ClientWidth; end;procedure TForm1.TrackBar1Change(Sender: TObject); beginBindingsList1.Notify(Sender, 'Value'); end;procedure TForm1.CheckBox1Change(Sender: TObject); beginbindExprItems.Active := TCheckBox(Sender).IsChecked; end;end.