• 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.
    

  • 相关阅读:
    Linux手动分区步骤
    Vue到底是怎样个框架?
    MongoDB
    25、正则表达式
    24、模块
    21、三元表达式、列表解析、生成器
    Linux 软件包 管理
    CentOS7.5---7.9 中文字体匹配错误 fontconfig-2.13.0
    Ubuntu14.04下Git安装与使用
    Zabbix3.4 安装配置
  • 原文地址:https://www.cnblogs.com/del/p/2198434.html
Copyright © 2020-2023  润新知