在Delphi下等这一功能很久了,虽然C#下早已实现了这一功能。但是在Dephi下尝试这项功能时还是有些许的激动。闲言少絮,直接上代码。

  1. unit BindingDemo;
  2.  
  3. interface
  4.  
  5. uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.Bind.EngExt, Vcl.Bind.DBEngExt,
  8. Vcl.StdCtrls, Data.Bind.Components, Vcl.Grids;
  9.  
  10. type
  11. TPerson = class(TObject)
  12. protected
  13. fName: string;
  14. fAge: integer;
  15. procedure
  16. SetName(const Value: string);
  17. public
  18. property Name: string read fName write SetName;
  19. property Age: integer read fAge write fAge;
  20. end;
  21.  
  22. type
  23. /// <summary>
  24. /// 一个object与ui控件绑定的简单例子
  25. /// </summary>
  26. TForm2 = class(TForm)
  27. bndscp1: TBindScope;
  28. bndngslst1: TBindingsList;
  29. Button1: TButton;
  30. Button2: TButton;
  31. edt1: TEdit;
  32. procedure btnLoadClick(Sender: TObject);
  33. procedure btnSaveClick(Sender: TObject);
  34. private
  35. fInitialized: boolean;
  36. fPerson: TPerson;
  37. procedure Initialize;
  38. { Private declarations }
  39. public
  40. { Public declarations }
  41. procedure AfterConstruction; override;
  42. procedure BeforeDestruction; override;
  43. end;
  44.  
  45. var
  46. Form2: TForm2;
  47.  
  48. implementation
  49.  
  50. {$R *.dfm}
  51.  
  52. procedure TForm2.AfterConstruction;
  53. begin
  54. inherited;
  55. Initialize;
  56. end;
  57.  
  58. procedure TForm2.BeforeDestruction;
  59. begin
  60. fPerson.Free;
  61. inherited;
  62. end;
  63.  
  64. procedure TForm2.btnLoadClick(Sender: TObject);
  65. begin
  66. fPerson.Name := 'Doogie Howser';
  67. fPerson.Age := ;
  68. bndscp1.DataObject := fPerson;
  69. end;
  70.  
  71. procedure TForm2.btnSaveClick(Sender: TObject);
  72. begin
  73. bndngslst1.Notify(edt1, '');
  74. end;
  75.  
  76. procedure TForm2.Initialize;
  77. var
  78. expression: TBindExpression;
  79. begin
  80. //Create a binding expression.
  81. expression := TBindExpression.Create(self);
  82. expression.ControlComponent := edt1;
  83. expression.ControlExpression := 'Text';
  84. //The Text property of Edit1 ...
  85. expression.SourceComponent := bndscp1;
  86. expression.SourceExpression := 'Name';
  87. //... is bound to the Name property of fPerson
  88. expression.Direction := TExpressionDirection.dirBidirectional;
  89. //Add the expression to the bindings list.
  90. expression.BindingsList := bndngslst1;
  91. //Create a Person object.
  92. fPerson := TPerson.Create;
  93. end;
  94.  
  95. { TPerson }
  96.  
  97. procedure TPerson.SetName(const Value: string);
  98. begin
  99. fName := Value;
  100. //ShowMessage('Name changed to "'+ Value +'"');
  101. end;
  102.  
  103. end.

http://blog.csdn.net/diligentcatrich/article/details/24552151

DelphiXe5中的双向绑定(使用使用TBindScope和TBindExpression,并覆盖AfterConstruction函数)的更多相关文章

  1. AngularJS中数据双向绑定(two-way data-binding)

    1.切换工作目录 git checkout step-4 #切换分支,切换到第4步 npm start #启动项目 2.代码 app/index.html Search: <input ng-m ...

  2. React中的“双向绑定”

    概述 React并不是一个MVVM框架,其实它连一个框架都算不上,它只是一个库,但是react生态系统中的flux却是一个MVVM框架,所以我研究了一下flux官方实现中的"双向绑定&quo ...

  3. vue中数据双向绑定注意点

    最近一个vue和element的项目中遇到了一个问题: 动态生成的对象进行双向绑定是失败 直接贴代码: <el-form :model="addClass" :rules=& ...

  4. vue中数据双向绑定的实现原理

    vue中最常见的属v-model这个数据双向绑定了,很好奇它是如何实现的呢?尝试着用原生的JS去实现一下. 首先大致学习了解下Object.defineProperty()这个东东吧! * Objec ...

  5. javascript中的双向绑定

    阅读目录 一:发布订阅模式实现数据双向绑定 二:使用Object.defineProperty 来实现简单的双向绑定. 前言: 双向数据绑定的含义:可以将对象的属性绑定到UI,具体的说,我们有一个对象 ...

  6. vue中的双向绑定

    概述 今天对双向绑定感兴趣了,于是去查了下相关文章,发现有用脏检查的(angular.js),有用发布者-订阅者模式的(JQuery),也有用Object.defineProperty的(vue),其 ...

  7. wp中的双向绑定

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...

  8. 利用JS实现vue中的双向绑定

    Vue 已经是主流框架了 它的好处也不用多说,都已经是大家公认的了 那我们就来理解一下Vue的单向数据绑定和双向数据绑定 然后再使用JS来实现Vue的双向数据绑定 单向数据绑定 指的是我们先把模板写好 ...

  9. AngularJS学习--- AngularJS中数据双向绑定(two-way data-binding) orderBy step4

    1.切换工作目录 git checkout step- #切换分支,切换到第4步 npm start #启动项目 2.代码 app/index.html Search: <input ng-mo ...

随机推荐

  1. [C#参考]属性

    属性和字段不同,属性是一个函数成员:它提供灵活的机制来读取.编写或计算某个私有字段的值. 可以像使用公共数据成员一样使用属性,但实际上它们是称作“访问器”的特殊方法. 这使得可以轻松访问数据,此外还有 ...

  2. [整理]DLL延时加载 && 设置进程私有环境变量

    DLL延时加载鉴于静态和动态加载之间,即无需在代码中显示加载但它内队依然是动态加载的方式只是系统帮处理了.这样做好处是: 1. 可以加快启动时间(因为它是动态加载在需要的时间加载), 2. 减小编写L ...

  3. Android 通过HTTPCLINET GET请求互联网数据

    private EditText et; private TextView tv; HttpClient client; @Override protected void onCreate(Bundl ...

  4. windows7 64位下运行 regsvr32 注册ocx或者dll的方法

    来源:转载   it won't work for you unless you have some form of Visual Basic tools loaded on your system: ...

  5. Flink资料(2)-- 数据流容错机制

    数据流容错机制 该文档翻译自Data Streaming Fault Tolerance,文档描述flink在流式数据流图上的容错机制. ------------------------------- ...

  6. Sencha Touch 之 Ext.ComponentManager.get方法使用

    HTML代码: <!doctype html> <html> <head> <meta charset="utf-8"> <t ...

  7. Oracle EBS-SQL (SYS-5):sys_配置文件查询.sql

    select    distinct l.profile_option_name,             v.profile_option_value,             fu.user_na ...

  8. freemaker

    FreeMarker模板文件主要由如下4个部分组成:  1,文本:直接输出的部分  2,注释:<#-- ... -->格式部分,不会输出  3,插值:即${...}或#{...}格式的部分 ...

  9. 1.padding和margin,几种参数

    这篇会很短. 那么如上图所示,margin指的是外边距,padding指的是内边距,border自有其像素宽度,element在1335乘以392的地方. margin和padding一样总共有四个, ...

  10. .Net 利用消息在进程间通讯实现进程互操作

    有时候我们会遇到需要在两个进程间通过某种方式实现互操作,方法有很多,例如你可以尝试让两个进程持续监视一个外部文件,由此文件记录各自进程的数据:还有可以使用网络端口实现进程间通讯.共享一片内存区域记录及 ...