• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

绑定表达式中可以有简单的运算和字符串连接, 但字符串需放在双引号中.
还可以使用 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()


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
使用MATLAB实现相机标定发布时间:2022-07-18
下一篇:
[MatLab]学习笔记3:变量及其操作发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap