2011-09-08 32 views

回答

9

看看样品。 SVN库网址: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属性的下拉列表中,选择“新活绑定...”,选择TBindExpression
  • 新建BindExpressionLabel11的编辑属性:指定标题到ControlExpression,BindScope1到SourceComponent,Edit1.Text + Edit2.Text到SourceExpression
+2

这看起来很棒。 SourceExpression ='Edit1.Text + Edit2.Text''如何编译,评估或者其他什么? –

+3

@David:有一个涉及到的整个运行时(可扩展)表达式评估引擎。 –

+0

哦,听起来很刺激! –

4

在(默认)示例项目的位置:

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

不正是。

+0

不完全是,它显示了一个源和一个控件组件之间的简单绑定。要在单个表达式中涉及两个不同的源组件,我相信您需要TBindScope组件来解析Edit1和Edit2引用。您也可以将Form1分配给SourceComponent,但这是使用全局变量Form1的所有后果。 –

+0

请注意,这些演示(LiveBindings的)只能通过ESD安装。 ISO映像不包含它们;如果你是从ISO安装的,你必须去他的回答中提供的链接TOndrej。 –

+0

@KenWhite - 演示也可以通过Subversion链接下载:http://sourceforge.net/projects/radstudiodemos/ –

-1

你不需要TBindScope将组件绑定在一起。假设你在表单上有edit1和edit2。如果您将edit2 BindingSource设置为edit1,则它将链接到edit1的更改

+0

他在询问有关将2个组件的属性添加在一起的问题。你的答案似乎是将一个组件的属性分配给另一个组件。 – chuacw