Delphi XE2 之 FireMonkey 入門(25) – 資料繫結: TBindingsList: 運算式的靈活性及運算式函數

來源:互聯網
上載者:User
綁定運算式中可以有簡單的運算和字串串連, 但字串需放在雙引號中.
還可以使用 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;interfaceuses  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()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.