您的位置:首页 > 编程语言 > Delphi

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

2013-09-30 10:36 706 查看

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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: