Delphi XE2 之 FireMonkey 入门(28) - 数据绑定: TBindingsList: 表达式函数测试: SelectedText()、CheckedState()
示例构想: 用 Label1 显示 ListBox1 的选项, 用 Label2 显示 CheckBox1 的状态.
1、放控件: Label1、Label2、ListBox1、CheckBox1、BindingsList1、BindScope1;
2、激活 ListBox1 的 OnClick 事件和窗体的默认事件.
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, FMX.Layouts, FMX.ListBox; type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; ListBox1: TListBox; CheckBox1: TCheckBox; BindingsList1: TBindingsList; BindScope1: TBindScope; procedure FormCreate(Sender: TObject); procedure ListBox1Click(Sender: TObject); end; var Form1: TForm1; implementation {$R *.fmx} uses Fmx.Bind.Editors; //使用表达式函数 SelectedText、SelectedItem、CheckedState 时, 需要的支持单元 procedure TForm1.FormCreate(Sender: TObject); var i: Integer; begin for i := 1 to 9 do ListBox1.Items.Add('Item_' + IntToStr(i)); with TBindExpression.Create(BindingsList1) do begin ControlComponent := Label1; ControlExpression := 'Text'; SourceComponent := BindScope1; SourceExpression := 'SelectedText(ListBox1)'; //同下一行 // SourceExpression := 'ListBox1.Selected.Text'; Active := True; end; with TBindExpression.Create(BindingsList1) do begin ControlComponent := Label2; ControlExpression := 'Text'; SourceComponent := BindScope1; SourceExpression := 'CheckedState(CheckBox1)'; //Checked: 'True'; Unchecked: 'False'; Grayed: '' // SourceExpression := 'CheckBox1.IsChecked'; //在本例中, 这等同于上一行 Active := True; end; CheckBox1.OnClick := ListBox1.OnClick; end; procedure TForm1.ListBox1Click(Sender: TObject); begin BindingsList1.Notify(Sender, ''); end; end.