A Look at LiveBindings for C++ and Delphi
Binding expressions are not a new concept, XAML and Flex developers by them. But outside of C++/CX, which is only used for WinRT programming, it isn’t something that normally seen in native programming languages. Designed for both VCL and FireMonkey, LiveBindings are the exception to the rule.
First some background. VCL or Visual Component Library is the old UI framework shared by Delphi and C++ Builder. It supports both x86 and x64 mode, but only on the Windows operating system. FireMonkey replaces VCL for new applications. It can be used to target both Windows and OS X. FileMonkey and VCL can be used in the same application, but not the same module.
According to Embarcadero,
LiveBindings is based on relational expressions, called binding expressions, that can be either unidirectional or bidirectional. LiveBindings is also about control objects and source objects. By means of binding expressions, any object can be bound to any other object, simply by defining a binding expression involving one or more properties of the objects you want to bind together. For example, you can bind a TEdit control to a TLabel so that, when the text changes in the edit box, the caption of the label is automatically adjusted to the value evaluated by your binding expression. Another example is binding a track bar control to a progress bar so that the progress rises or lowers as you move the track bar.
Like many of the Delphi/C++ Builder tools, LiveBindings are generally created using a GUI tool. There are two options here, the LiveBindings Designer and the LiveBindings Wizard. This will undoubtedly aggravate developers who are used to manually editing binding expressions in XAML or Flex.
Note: It is possible to edit bindings programmatically, but this is strongly discouraged. The documentation for it is buried inside a tutorial on console applications.
Unlike .NET, which requires objects to implement a somewhat complex design patterns, objects in Delphi and C++ Builder are bindable by default. Like practically all objects, you simple need to subclass TObject and then expose properties like normal. For C++ Builder, this means using the __property extension keyword.
The code for actually applying binding expression is quite verbose, but it does allow complex expressions. Here we see that the expressions 'o1.IntegerValue + o2.IntegerValue' are being bound to the target property MyResultObject.IntegerValue.
In XAML technologies, binding expressions are based on events. In order to avoid memory leaks, they internally use a “weak events” structure to monitor PropertyChanged events that need to be relayed through binding expressions. This structure requires that all bindings be centrally registered, a fact that most developers are not aware of.
LiveBindings lack the concept of a property changed event, so a similar abstraction is not possible. Bindings are still centrally registered, but the binding engine needs to be explicitly notified when a property is changed. This is done using the TBindings.Notify function, which accepts an object and a property name.
In theory the TBindings.Notify function can be invoked by the property setter, eliminating the need for client code to invoke it explicitly. This raises questions about multi-threading, which the documentation doesn’t cover, but does eliminate the need for weak events.
Camille Fournier May 21, 2015