假设我想将一个TPerson.PersonName:string绑定到一个TEdit.Text。
现在我现在很简单
>创建一个新的VCL应用程序,添加一个TBindScope,TBindingsList,TEdit。
>创建名为person1的TPerson实例。
>使用BindingList,添加一个TBindExpression属性。
>使用BindExpression
>
>将ControlComponent设置为Edit1
>
>将ControlExpression设置为’Text’
>
>将SourceComponent设置为BindScope1
>
将SourceExpression设置为PersonName
>添加按钮;到Click事件我添加:BindScope1.DataObject:= person1;
>添加按钮;到点击事件我添加(只有一个是必要的,但直到它的工作,我会尝试他们)。
>
TBindings.Notify(发件人,”);
>
> BindingsList1.Notify(sender,”);
第一个按钮在第一个方向上绑定。第二个按钮从来没有似乎将该值写回person1.PersonName属性。
我已经尝试了通知代码,绑定方向,绑定类型,表达式,SourceMember等。有时我在bindexpression配置中遇到运行时错误,其余的时间绑定只是单向的。
我希望单击第二个按钮,并将Edit1.Text的内容写入person1.PersonName。
如果我必须从代码中完成所有这些,我会考虑它,这样的例子是受欢迎的,但是我真的想通过设计师来做到这一点。
请注意,我对两个控件之间的绑定不感兴趣。
也请注意,我已经下载并检查了LiveBinding示例项目,没有找到任何这样做。如果这是错误的,请具体指出它。我也读过DocWiki。它不包括双向绑定,除了使用DB LiveBinding控件。我没有使用DB LiveBinding控件,也没有使用DataSet。所以除非你可以向我解释为什么要使用它们,我不需要有关这些控件的任何信息。
解决方法
创建一个VCL表单项目。在表单上,删除这些表单上的每一个:
TBindScope
TBindingsList
TButton的
TButton的
TEDIT
将其中一个按钮重命名为btnLoad,而将其他按钮重命名为btnSave。
将此代码粘贴到您的窗体单元(假定它命名为Form1)。分配按钮的点击处理程序并运行它。单击btnLoad以使用TPerson对象数据填充编辑框,将编辑框中的文本编辑为新值,然后单击btnSave将其写回TPerson对象。
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.