2011-09-08 4 views
10

컨트롤간에 라이브 바인딩을 수행 할 수 있습니까? 즉, 두 개의 편집 상자를 사용하여 내용을 레이블에 함께 추가 한 결과를 가져옵니다.Delphi XE2 및 컨트롤 간의 라이브 바인딩

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

정확하게 그 작업을 수행합니다 나는

감사

답변

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

: 결과를 생성하는 IDE 디자이너를 사용하는 방법

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. 
  • 두 개의 편집 (Edit1, Edit2), 레이블 (Label1) 및 TBindScope (BindScope1)을 양식 (Form1)에 삽입하십시오.
  • 두 편집의 OnChange 이벤트 (EditChange)에 대한 이벤트 처리기를 만듭니다.
  • 선택 TBindExpression, '... 새로운 라이브 바인딩'을 선택, LiveBindings 속성의 드롭 다운을 확장, 새로 만든 BindExpressionLabel11의
  • 편집 속성을 Label1을 선택 SourceComponent, Edit1.Text에 ControlExpression, BindScope1에 캡션을 할당 + Edit2.Text to SourceExpression
+2

꽤 멋져 보입니다. 'SourceExpression = 'Edit1.Text + Edit2.Text''는 어떻게 컴파일되고 평가 되나요? –

+3

@David : 전체 런타임 (확장 가능) 식 평가 엔진이 관련되어 있습니다. –

+0

호쾌한 소리! –

4

(기본값)에서 샘플 프로젝트의 위치를 ​​어디서부터 시작 난 그냥 모르는, 그것이 확신합니다.

+0

정확히 하나의 소스와 하나의 컨트롤 구성 요소 사이에 간단한 바인딩이 표시됩니다. 단일 표현식에 포함 된 두 개의 별개의 소스 구성 요소를 얻으려면 Edit1 및 Edit2 참조를 해결하기 위해 TBindScope 구성 요소가 필요하다고 생각합니다. Form1을 SourceComponent에 할당 할 수도 있지만 모든 결과와 함께 전역 변수 Form1을 사용합니다. –

+0

이 데모 (라이브 바인딩)는 ESD 설치를 통해서만 사용할 수 있습니다. ISO 이미지에 포함되어 있지 않습니다. ISO에서 설치 한 경우 해당 답변에서 제공된 TOndrej 링크로 이동해야합니다. –

+0

@KenWhite - 데모는 Subversion 링크를 통해 다운로드 할 수도 있습니다. http://sourceforge.net/projects/radstudiodemos/ –

-1

TBindScope를 사용하여 구성 요소를 바인딩 할 필요가 없습니다. 양식에 edit1 및 edit2가 있다고 가정 해보십시오. edit2 BindingSource를 edit1로 설정하면 edit1에 대한 변경 사항에 대한 링크가됩니다.

+0

두 구성 요소의 속성을 함께 추가하는 방법에 대해 질문했습니다. 귀하의 대답은 한 구성 요소의 속성을 다른 구성 요소에 할당하는 것과 같습니다. – chuacw

관련 문제