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.

转载于:https://www.cnblogs.com/dzdd/p/3346899.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值