.NET databinding in Delphi for Win32

CodeGear, Delphi for Win32, Design Patterns, MVP, Programming, Uncategorized Add comments

Databinding is defined as: “General technique that binds two data/information sources together and maintains them in sync. This is usually done with two data/information sources with different types as in XML data binding. However in UI data binding, we bind data and information objects of the same type together (e.g. Java objects to Java UI elements).”

Databinding is common technique in VCL. Since Delphi 1 we have TDataset class for bind data and UI controls (DB Aware) in a GUI application.

In .NET world, instead, databinding is very different.

So, I’m starting to write (actually for fun) a DataBinder component to use .NET “like” databinding (or something similar to) in Delphi for Win32 too.

All the code has been written in about 2 hours.

TDataBinder

With this component you can “bind” an object property to another object property in a declarative mode.

e.g.

  1. DataBinder.Add(Person, 'FirstName', Edit1, 'Text');

and then, every update to Person.FirstName property, will be reflected in the Edit1.Text property.

You can bind different control properties to different BO properties.

e.g.

  1. //Text = FirstName
  2. DataBinder.Add(Person, 'FirstName', Edit1, 'Text');
  3. //If Person is not married, TEdit become flat
  4. DataBinder.Add(Person, 'IsMarried', Edit1, 'Ctl3D');

So in your initialization code (e.g. FormCreate) you can write somethig similat to following:

  1. procedure TForm3.FormCreate(Sender: TObject);
  2. var
  3. binder: TDataBinder;
  4. begin
  5. //Create your "BO"
  6. Person := TPerson.Create;
  7. //read data from "database"
  8. Person.Load;
  9.  
  10. //Setup databinding…
  11. binder := TDataBinder.Create(self);
  12. binder.Add(Person, 'FirstName'   ,      Edit1,     'Text');
  13. binder.Add(Person, 'LastName',          Edit2,     'Text');
  14.  
  15. //The same attribute binded to 3 controls
  16. binder.Add(Person, 'Married',     CheckBox1, 'Checked');
  17. binder.Add(Person, 'Married'   , Edit1,     'Ctl3D');
  18. binder.Add(Person, 'Married',     Edit2,     'Ctl3D');
  19.  
  20. //The same attribute binded to 2 controls
  21. binder.Add(Person, 'SomeInteger', ComboBox1, 'ItemIndex');
  22. binder.Add(Person, 'SomeInteger',     TrackBar1, 'Position');
  23.  
  24. //A derived property
  25. binder.Add(Person, 'FullName',     Panel1, 'Caption');
  26.  
  27. //let start…
  28. binder.Bind;
  29. end;

Other info asap so, stay tuned.

Download Code and compiled sample

(Source code require Delphi 2009)

3 Responses to “.NET databinding in Delphi for Win32”

  1. Franco Damiani Says:

    Very nice, good luck for this blog!
    It could become a substitution for my daily-questions
    to you! :D :D :D

  2. while true do; » Blog Archive » In the core of LiveBindings expressions of RAD Studio XE2 Says:

    [...] I’ve waited LiveBindings for ages and now they are here! [...]

  3. Mostafa Says:

    Thank you!I emolpy different techniques when I find these debugging problems, but it mostly works. The newer Delphi versions are pretty stable in this regard.Alex.

Leave a Reply

You must be logged in to post a comment.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in