Delphi XE2 之 FireMonkey 入門(29) - 數據綁定: TBindingsList: 表達式的 Evaluate() 方法


TBindingsList 中可能不止一個表達式, 經過表達式的 Evaluate 方法可單獨提交綁定, 並可在 Active = False 時提交.
在 TBindExprItems 中對應的方法是 EvaluateFormat.

測試設想: Label一、Label2 的綁定源同是 Edit1, 分別提交綁定.

一、在窗體上加控件: Label一、Label二、Edit一、BindingsList1;
二、激活 Edit1 的 OnKeyUp、OnChange 事件, 還有窗體的默認事件.

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Edit, Data.Bind.EngExt,
  FMX.Bind.DBEngExt, Data.Bind.Components;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    BindingsList1: TBindingsList;
    procedure FormCreate(Sender: TObject);
    procedure Edit1KeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
    procedure Edit1Change(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

var
  bindExpression1, bindExpression2: TBindExpression;

procedure TForm1.FormCreate(Sender: TObject);
begin
  bindExpression1 := TBindExpression.Create(BindingsList1);
  bindExpression2 := TBindExpression.Create(BindingsList1);

  with bindExpression1 do
  begin
    ControlComponent := Label1;
    ControlExpression := 'Text';
    SourceComponent := Edit1;
    SourceExpression := 'Text';
    // Active := True;
  end;

  with bindExpression2 do
  begin
    ControlComponent := Label2;
    ControlExpression := 'Text';
    SourceComponent := Edit1;
    SourceExpression := 'UpperCase(Text)';
    // Active := True;
  end;
end;

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState);
begin
  bindExpression1.Evaluate;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  bindExpression2.Evaluate;
end;

end.
相關文章
相關標籤/搜索