Delphi XE2 之 FireMonkey 入门(24) - 数据绑定: TBindingsList: TBindExpression.Direction


在学习 BindingSource 属性时, 可以让两个控件互为绑定源; TBindExpression 对应的功能是 Direction 属性.

先在窗体上添加 Edit1、Edit2、BindingsList1; 然后激活 Edit1、Edit2 和窗体的默认事件.


 

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TBindExpression.Create(BindingsList1) do
  begin
    ControlComponent := Edit2;
    ControlExpression := 'Text';
    SourceComponent := Edit1;
    SourceExpression := 'Text';
//    Direction := TExpressionDirection.dirSourceToControl; //默认
//    Direction := TExpressionDirection.dirControlToSource; //反向
    Direction := TExpressionDirection.dirBidirectional;     //双向
    Active := True;
  end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  BindingsList1.Notify(Sender, '');
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
  BindingsList1.Notify(Sender, '');
end;


如果没有 Direction 属性, 代码或许会是:


 

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TBindExpression.Create(BindingsList1) do
  begin
    ControlComponent := Edit2;
    ControlExpression := 'Text';
    SourceComponent := Edit1;
    SourceExpression := 'Text';
    Active := True;
  end;

  with TBindExpression.Create(BindingsList1) do
  begin
    ControlComponent := Edit1;
    ControlExpression := 'Text';
    SourceComponent := Edit2;
    SourceExpression := 'Text';
    Active := True;
  end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  BindingsList1.Notify(Sender, '');
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
  BindingsList1.Notify(Sender, '');
end;
posted @ 2013-09-30 10:35  独自等待√  阅读(285)  评论(0编辑  收藏  举报