首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要控件和对象之间的双向LiveBindings

需要控件和对象之间的双向LiveBindings
EN

Stack Overflow用户
提问于 2011-09-20 00:19:01
回答 1查看 6.6K关注 0票数 20

在Delphi XE2 LiveBindings中,我需要将任意类型的VCL控件绑定到任意(非组件)对象上的任意类型的属性。我可以单向地做。但我需要双向地做。

假设我想将TPerson.PersonName: string绑定到TEdit.Text。

我现在所拥有的很简单。

  • set
    • 创建一个新的VCL应用程序,添加一个TBindScope、TBindingsList、TEdit。
    • 创建一个TPerson实例,名为person1.
    • ,使用BindingList添加TBindExpression属性。

    • set ControlExpression to 'Text'

    • set SourceComponent to BindScope1

    • set SourceExpression to PersonName

  • 添加一个按钮;向单击事件添加: BindScope1.DataObject := person1;
  • 添加一个按钮;向Click事件添加一个按钮(只需要一个按钮,但在运行之前,我将尝试它们)

    • BindingsList1.Notify(sender,'');

第一个按钮向第一个方向绑定。第二个按钮似乎从未将值写回person1.PersonName属性。

我已经尝试过通知代码、绑定方向、绑定类型、表达式、SourceMember等。有时在bindexpression中会出现运行时错误,其余时间绑定只是单向的。

我希望点击第二个按钮,并看到Edit1.Text被写入person1.PersonName的内容。

如果我必须从代码中完成这一切,我将考虑它,这样的例子是受欢迎的,但是如果可能的话,我真的很想通过设计者来完成它。

请注意,我对两个控件之间的绑定不感兴趣。

请注意,我已经下载并检查了LiveBinding示例项目,但没有找到任何这样做的项目。如果这是错误的,请明确指出它。我也读过DocWiki。它不包括双向绑定,除非使用DB LiveBinding控件。我不使用DB LiveBinding控件,也不使用DataSet。所以,除非你能向我解释我为什么要使用这些控件,否则我就不需要任何有关这些控件的信息。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-21 22:13:02

我现在已经开始工作了。我在设计人员中完成了所有的工作,但我已经将其转换为大部分代码,以便更好地共享它。

创建一个VCL窗体项目。在表单上,将其中的每一个放在表单上:

TBindScope TBindingsList TButton TButton TEdit

将其中一个按钮重命名为btnLoad,另一个重命名为btnSave。

将此代码粘贴到表单单元上(假设它名为Form1)。为按钮分配单击处理程序并运行它。单击btnLoad以使用TPerson对象数据填充编辑框,将编辑框中的文本编辑为新值,然后单击btnSave将其写回TPerson对象。

代码语言:javascript
复制
unit Form1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Rtti,

  // LiveBinding units
  System.Bindings.Helper,     // Contains TBindings class
  Data.Bind.EngExt,
  Vcl.Bind.DBEngExt,
  Data.Bind.Components,
  System.Bindings.Outputs;

type
  TPerson = class(TObject)
  protected
    fName: string;
    fAge: integer;
    procedure SetName(const Value: string);
  public
    property Name: string read fName write SetName;
    property Age: integer read fAge write fAge;
  end;

type
  TForm1 = class(TForm)
    btnLoad: TButton;
    btnSave: TButton;
    BindScope1: TBindScope;
    BindingsList1: TBindingsList;
    Edit1: TEdit;
    procedure btnLoadClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  private
    fInitialized: boolean;
    fPerson: TPerson;
    procedure Initialize;
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.AfterConstruction;
begin
  inherited;
  Initialize;
end;

procedure TForm1.BeforeDestruction;
begin
  fPerson.Free;
  inherited;
end;

procedure TForm1.btnLoadClick(Sender: TObject);
begin
  fPerson.Name := 'Doogie Howser';
  fPerson.Age := 15;
  BindScope1.DataObject := fPerson;
end;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
  TBindings.Notify(Edit1, '');

  // Could also do this:
  //BindingsList1.Notify(Edit1, '');
end;

procedure TForm1.Initialize;
var
  expression: TBindExpression;
begin
  // Create a binding expression.
  expression := TBindExpression.Create(self);
  expression.ControlComponent := Edit1;
  expression.ControlExpression := 'Text';   // The Text property of Edit1 ...
  expression.SourceComponent := BindScope1;
  expression.SourceExpression := 'Name';    // ... is bound to the Name property of fPerson
  expression.Direction := TExpressionDirection.dirBidirectional;

  // Add the expression to the bindings list.
  expression.BindingsList := BindingsList1;

  // Create a Person object.
  fPerson := TPerson.Create;
end;

{ TPerson }

procedure TPerson.SetName(const Value: string);
begin
  fName := Value;
  ShowMessage('Name changed to "'+ Value +'"');
end;

end.
票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7478785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档