Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
DelphiIDENews

Delphi Binding Expressions, Properties and Components

Continuing last week blog post on binding expressions, let’s see how components can participate, but creating expressions that refer to properties.


Last week I introduced Delphi RTL expression engine in the blog post at https://blog.marcocantu.com/blog/2021-may-delphi-expression-engine.html. Now let’s make an additional step. I want to explain how components and their properties can be part of these expressions. In this case we don’t simply evaluate an expression, but want to associate two separate expressions in a “binding”.

Binding Two Components

In practical terms, suppose you have a form with a SpinEdit and a ProgressBar. A managed binding allows you to provide an input expression (with one or more input objects) and an output expression (again with one or more objects). As you can see in the code below, each object is associate to the expression providing a name, like spin1 for the SpinEdit1 component, and the expression can refer to the object and its properties, as in spin1.Value. The same happens for the output. You can also specify an output converter, which I’ve omitted here:

With this configuration code, if you call BindingExpression1.Evaluate the value of the SpinEdit will be copied to the ProgressBar. You can make the same call to refresh the value, but you can also abstract the concept by indicating to the bindings architecture that the value property of the spin edit has changes. The system will automatically determine if there is one or more expressions involving the property and refresh it:

This change of perspective (a value has changed rather than an expression needs to be recalculated) is of a fundamental importance, as it fully abstracts the data model from the UI it is associated with. We’ll get to it, but let me show the simple application UI first:

controlsbinding-3076596

Binding Objects and UI Controls

This is better explained in the second part of the example, which has an edit box bound to an object in memory. The TMyObject class has a Name and a Value property. Here is how you can bind an object to the UI:

In this code MyObj is an object of the TMyObject class. Its Name property is associate with the edName component Text. Again, refreshing the expression updates the output. But the object should have no knowledge of the user interface controls or the UI bindings. In fact, all you need to do to instrument the object is to notify the system that the value has changed:

With this code, when the data changes, the UI refreshes automatically to reflect it:

I’m not 100% sure how you want to call the patterns implemented with bindings in Delphi, but it certainly offers a complete separation and abstraction of the data model with the user interface view.

There is Much More To Explore

Next, we can start looking into how you can work with bi-directional bindings, and how you can define all of the logic above using specific component designers, reducing the code required. There is a lot of technology behind Visual Live Bindings, which is the potential ultimate destination and what we often discuss. Live Bindings are nice, but the binding technology in the Delphi RTL is where the power lives.

rad-studio-banner-blog


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

About author

Marco is one of the RAD Studio Product Managers, focused on Delphi. He's the best selling author of over 20 books on Delphi.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES