随笔 - 2146  文章 - 19 评论 - 11846 阅读 - 1267万


示例构想: 用 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.

posted on   万一  阅读(3804)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧


点击右上角即可分享
微信分享提示