Delphi XE2和控件之间的LiveBindings

10

能否在控件之间进行实时绑定,比如将2个编辑框的内容相加并显示在标签中。我相信这是可能的,只是不知道从何处开始。

谢谢

3个回答

9
请查看示例。SVN存储库URL:https://radstudiodemos.svn.sourceforge.net/svnroot/radstudiodemos/branches/RadStudio_XE2/LiveBindings 一个例子:
----- Unit1.dfm -----
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 286
  ClientWidth = 426
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 8
    Top = 62
    Width = 48
    Height = 13
    Caption = 'Edit1Edit2'
  end
  object Edit1: TEdit
    Left = 8
    Top = 8
    Width = 121
    Height = 21
    TabOrder = 0
    Text = 'Edit1'
    OnChange = EditChange
  end
  object Edit2: TEdit
    Left = 8
    Top = 35
    Width = 121
    Height = 21
    TabOrder = 1
    Text = 'Edit2'
    OnChange = EditChange
  end
  object BindingsList1: TBindingsList
    Methods = <>
    OutputConverters = <>
    UseAppManager = True
    Left = 20
    Top = 5
    object BindExpressionLabel11: TBindExpression
      Category = 'Binding Expressions'
      ControlComponent = Label1
      SourceComponent = BindScope1
      SourceExpression = 'Edit1.Text + Edit2.Text'
      ControlExpression = 'Caption'
      NotifyOutputs = False
      Direction = dirSourceToControl
    end
  end
  object BindScope1: TBindScope
    Left = 192
    Top = 16
  end
end

----- Unit1.pas -----

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.Bind.EngExt, Vcl.Bind.DBEngExt,
  System.Rtti, System.Bindings.Outputs, Vcl.Bind.Editors, Data.Bind.Components,
  Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    BindingsList1: TBindingsList;
    BindExpressionLabel11: TBindExpression;
    BindScope1: TBindScope;
    procedure EditChange(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  System.Bindings.Helper;

procedure TForm1.EditChange(Sender: TObject);
begin
  TBindings.Notify(Sender, 'Text');
end;

end.

如何使用IDE设计器来实现以下结果:

  • 在表单(Form1)上放置两个编辑框(Edit1, Edit2), 一个标签(Label1)和一个TBindScope(BindScope1)。
  • 为两个编辑框的OnChange事件(EditChange)创建事件处理程序。
  • 选择Label1,展开LiveBindings属性下拉列表,选择“新建Live绑定(New Live Binding...)”,选中TBindExpression。
  • 编辑新创建的BindExpressionLabel11的属性:将Caption分配给ControlExpression,将BindScope1分配给SourceComponent,将Edit1.Text + Edit2.Text分配给SourceExpression。

2
看起来非常棒。SourceExpression = 'Edit1.Text + Edit2.Text' 是如何编译、评估或执行的? - David Heffernan
3
@David:整个运行时(可扩展)表达式评估引擎都参与其中。 - Ondrej Kelle
如果我没记错的话,它不在调色板上,而是由实时绑定编辑器创建。 - Ondrej Kelle
抱歉 - 我不记得了,而且我也没有XE2了。 - Ondrej Kelle
在一些RAD Studio版本中,TBindScope已经消失了,但它又随着RAD Studio 10.4.1版本的推出而回归了。 - Didier Cabalé
显示剩余2条评论

4

这个示例项目位于(默认)位置:

C:\Users\Public\Documents\RAD Studio\9.0\Samples\Delphi\LiveBinding\Components\bindexpression\fmx\BindExpressionSampleProject.dproj

确切地做到这一点。


并不完全如此,它仅显示了一个源组件和一个控件组件之间的简单绑定。如果要将两个不同的源组件涉及到单个表达式中,我认为您需要使用TBindScope组件来解析Edit1和Edit2引用。还可以将Form1分配给SourceComponent,但这会使用具有所有后果的全局变量Form1。 - Ondrej Kelle
请注意,这些演示(LiveBindings 演示)仅在 ESD 安装中提供。ISO 映像不包含它们;如果您从 ISO 安装,请前往 TOndrej 在他的回答中提供的链接。 - Ken White
@KenWhite -- 这些演示也可以通过Subversion链接下载:http://sourceforge.net/projects/radstudiodemos/。 - Nick Hodges
@Nick:“这个项目没有文件”,来自您提供的链接。从那里有一个到svn页面的链接,这是TOndrej发布的相同链接。我错过了什么吗? - Ken White

-1

你不需要使用TBindScope将组件绑定在一起。假设在表单上有edit1和edit2两个控件。如果你将edit2的BindingSource设置为edit1,那么当edit1发生更改时,它也会被应用到edit2中。


他在询问如何将两个组件的属性相加。你的回答似乎是关于将一个组件的属性赋值给另一个组件。 - chuacw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接