2015-01-30 2 views
1

livebindings를 사용하여 내 (구성 요소가 아닌) 객체의 속성을 업데이트하려고합니다. 구성 요소를 내 개체 필드에 바인딩하고 런타임에 TObjectBindSourceAdapter를 사용하는 TPrototypeBindSource가 있습니다. 편집 컴포넌트의 onchange 이벤트에서 MyPrototypeBindSource.Refresh으로 호출하면 작동하지만, 폼의 모든 컴포넌트에 대해 onchange 이벤트를 설정하지 않고 자동으로이 작업을 수행 할 수있는 방법이 있습니까?TPrototypeBindSource를 사용하여 객체의 속성을 자동으로 업데이트

+1

단일 OnChange 이벤트를 만들고 모든 편집 컨트롤에 연결하는 것을 제외하면 다른 의미입니까? –

+0

그건 나에게 발생하지 않았지만 실제 바인딩을 수동으로 새로 고치지 않고 변경 사항을 푸시 할 수 있다고 생각했습니다. 아니면 내가 어떻게 작동하는지 착각 한거야? –

+0

LiveBindings에 대한 내 경험이 몇 가지 작은 응용 프로그램으로 제한된다는 것을 인정해야하지만 그렇게 할 수는 없었습니다. (우리는 여전히 대부분의 응용 프로그램을 지원하지 않는 이전 버전의 Delphi에서 사용하고 있습니다.) –

답변

3

데이터 개체에 대한 제어 데이터의 자동 게시를 처리하기 위해 suspectTPrototypeBindSource.AutoPost이 있지만 원본을 잘 보면이 속성은 내부 데이터 생성기에 영향을줍니다.

procedure TForm1.PrototypeBindSource1CreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter); 
begin 
    FPerson := TPerson.Create; 
    ABindSourceAdapter := TObjectBindSourceAdapter<TPerson>.Create(Self, FPerson); 
    ABindSourceAdapter.AutoEdit := True; 
    ABindSourceAdapter.AutoPost := True; 
end; 

이이 일을 할 것입니다 :

우리가 (우리는이 시점에서 단지이기 때문에 우리는뿐만 아니라 AutoEdit를 설정합니다) 어댑터를 만들 때 손으로이 속성을 설정해야 할 것 같습니다 때마다 TEdit으로 두지 만 TCheckBox 은 즉시 데이터를 게시합니다.

이 단지 published 방법

procedure TForm1.ControlChanged(Sender: TObject); 
begin 
    if Sender is TComponent 
    then 
    TLinkObservers.ControlChanged(Sender as TComponent); 
end; 

를 사용하여 데이터 객체에 곧바로 데이터를 얻기 위해 각각의 필요한 제어 (예를 들면 TEdit.OnChange)이 할당을 변경하는 방법. 당신이를 게시하면 TPrototypeBindSource을 강제 할 수있는 모든 컨트롤에 ControlChanged 방법을 지정하는 마음에 들지 않으면

 
Active : ftBoolean -> CheckBox1/CheckedState(Self) 
Firstname : ftString -> Edit1/Text 
Lastname : ftString -> Edit2/Text 

: 여기

하나의 전체가

type 
    TPerson = class 
    private 
    FFirstname: string; 
    FLastname: string; 
    FActive: Boolean; 
    public 
    function ToString: string; override; 

    property Active: Boolean read FActive write FActive; 
    property Firstname: string read FFirstname write FFirstname; 
    property Lastname: string read FLastname write FLastname; 
    end; 

    TForm1 = class(TForm) 
    PersonSource: TPrototypeBindSource; { OnCreateAdapter -> PersonSourceCreateAdapter } 
    Edit1: TEdit; { OnChange -> ControlChanged } 
    Edit2: TEdit; { OnChange -> ControlChanged } 
    BindingsList1: TBindingsList; 
    LinkControlToField1: TLinkControlToField; 
    LinkControlToField2: TLinkControlToField; 
    Label1: TLabel; 
    ApplicationEvents1: TApplicationEvents; { OnIdle -> ApplicationEvents1Idle } 
    CheckBox1: TCheckBox; 
    LinkControlToField3: TLinkControlToField; 
    procedure PersonSourceCreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter); 
    procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean); 
    private 
    FPerson: TPerson; 
    published 
    procedure ControlChanged(Sender: TObject); 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

procedure TForm1.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean); 
begin 
    // just for checking then object data 
    Label1.Caption := FPerson.ToString; 
end; 

procedure TForm1.ControlChanged(Sender: TObject); 
begin 
    if Sender is TComponent 
    then 
    TLinkObservers.ControlChanged(Sender as TComponent); 
end; 

procedure TForm1.PersonSourceCreateAdapter(Sender: TObject; var ABindSourceAdapter: TBindSourceAdapter); 
begin 
    FPerson := TPerson.Create; 
    ABindSourceAdapter := TObjectBindSourceAdapter<TPerson>.Create(Self, FPerson); 
    ABindSourceAdapter.AutoEdit := True; 
    ABindSourceAdapter.AutoPost := True; 
end; 

{ TPerson } 

function TPerson.ToString: string; 
begin 
    Result := FLastname + ', ' + FFirstname + ' ' + BoolToStr(FActive); 
end; 

LiveBindings 이동 데이터를 TPrototypeBindSource.Post으로 호출하여 이 당신이 데이터를해야 할 때마다

if PersonSource.Editing 
then 
    PersonSource.Post; 

전화 게시 ... 단지 TApplicationEvents.OnIdle 내에서 전화를 언제든지 경우 :하지만 당신은 편집 모드에 있는지 먼저 확인해야합니다.

+0

AutoPost 설정이 Prorotype에서 이어질 것이라고 가정 했었습니다. 대부분의 경우 종료 할 때 대부분 사용하기에 충분합니다. 감사. –

+0

당신은 그걸로 혼자가 아닙니다. 나는 왜 그것이 소스가 아닌지를 봐야했습니다 : o) –