Delphi XE2 之 FireMonkey 入门(25) - 数据绑定: TBindingsList: 表达式的灵活性及表达式函数...


绑定表达式中可以有简单的运算和字符串连接, 但字符串需放在双引号中.
还可以使用 TBindingsList.Methods 提供的一组表达式函数(分别来自 System.Bindings.Methods 和 Data.Bind.EngExt 单元):
ToStr()
ToVariant()
Round()
Format()
UpperCase()
LowerCase()
FormatDateTime()
StrToDateTime()
Max()
Min()
CheckedState()
SelectedItem()
SelectedText()


示例: 用三个 TLabel 分别呈现窗体的宽度、高度、面积.

现在窗体上添加 Label1、Label2、Label3、BindingsList1, 并激活窗体的 OnCreate 和 OnResize 事件:

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;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    BindingsList1: TBindingsList;
    procedure FormCreate(Sender: TObject);
    procedure FormResize(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 := Form1;
    SourceExpression := '"宽度: " + ToStr(Width)';
    Active := True;
  end;

  with TBindExpression.Create(BindingsList1) do
  begin
    ControlComponent := Label2;
    ControlExpression := 'Text';
    SourceComponent := Form1;
//    SourceExpression := '"高度: " + ToStr(Height)';
    SourceExpression := 'Format("高度: %s", ToStr(Height))'; //同上一行; 在表达式中使用 Format 函数时, 后面的参数不能放在 [] 中
    Active := True;
  end;

  with TBindExpression.Create(BindingsList1) do
  begin
    ControlComponent := Label3;
    ControlExpression := 'Text';
    SourceComponent := Form1;
    SourceExpression := '"面积: " + ToStr(Width * Height)';
    Active := True;
  end;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  BindingsList1.Notify(Sender, 'Width');
  BindingsList1.Notify(Sender, 'Height');
end;

end.


在表达式中还可以使用关键字 Self、Owner.

参考: Delphi XE2 之 FireMonkey 入门(28) - 数据绑定: TBindingsList: 表达式函数测试: SelectedText()、CheckedState()

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值