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()
相关文章推荐
- Delphi XE2 之 FireMonkey 入门(24) - 数据绑定: TBindingsList: TBindExpression.Direction
- Delphi XE2 之 FireMonkey 入门(23) - 数据绑定: TBindingsList: TBindExpression
- Delphi XE2 之 FireMonkey 入门(22) - 数据绑定: BindingSource、BindingName、FindBinding()、Binding[]
- Delphi XE2 之 FireMonkey 入门(21) - 和 FMX 相关的类(表)
- Delphi XE2 之 FireMonkey 入门(20) - TStyleBook(皮肤、样式相关)
- Delphi XE2 之 FireMonkey 入门(18) - TLang(多语言切换的实现)
- Delphi XE2 之 FireMonkey 入门(17) - 特效
- Delphi XE2 之 FireMonkey 入门(16) - 滤镜: 实例测试
- Delphi XE2 之 FireMonkey 入门(15) - 滤镜: 获取滤镜信息
- Delphi XE2 之 FireMonkey 入门(14) - 滤镜: 概览
- Delphi XE2 之 FireMonkey 入门(13) - 动画(下)
- Delphi XE2 之 FireMonkey 入门(12) - 动画(上)
- Delphi XE2 之 FireMonkey 入门(11) - 控件居中、旋转、透明
- Delphi XE2 之 FireMonkey 入门(10) - 常用结构 TPoint、TPointF、TSmallPoint、TSize、TRect、TRectF 及相关方法
- Delphi XE2 之 FireMonkey 入门(9) - TBitmap
- Delphi XE2 之 FireMonkey 入门(8) - TImage
- Delphi XE2 之 FireMonkey 入门(7) - TText 与 TFont
- Delphi XE2 之 FireMonkey 入门(6) - TLine、TEllipse、TCircle、TPie、TArc、TRectangle、TRoundRect、TCalloutRectangle
- Delphi XE2 之 FireMonkey 入门(5) - TAlphaColor
- Delphi XE2 之 FireMonkey 入门(4) - 控件天生可做容器