LiveBindings in RAD Studio

From RAD Studio XE2
Jump to: navigation, search

Go Up to Getting Started with RAD Studio

LiveBindings is a data-binding feature supported by both the VCL and FireMonkey in RAD Studio. LiveBindings is expression-based, which means it uses expressions to bind objects to each other, by means of their properties.

LiveBindings Overview

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 source object can be bound to itself (it becomes both source and control object) or to any other control 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 tracker control to a progress bar so that the progress rises or lowers as you move the track bar.

In the same manner, you can connect to databases, alter one or more properties of different objects, and so on. Because LiveBindings propagates, you can even alter properties of objects that are connected to other objects that are bound to a control object.

LiveBindings Usage

You can use LiveBindings in VCL and FMX applications by means of the Object Inspector and the visual components used by LiveBindings. However, there is a way to use LiveBindings programatically in Console Applications.

The following tutorials show the basics of using LiveBindings:

LiveBindings can be managed or unmanaged. Managed bindings are automatically evaluated when an object or a property in the input expression is modified. For example, if the input expression references the Text property of an edit box component called Edit1, the binding will be evaluated when the expression engine is informed of a change to Edit1. TBindingList.Notify is called to notify the expression engine. The first parameter to Notify() is the object (Edit1, in this case) and the second parameter is the affected property. The property parameter can be blank, in order to cause all bindings that reference the object to be evaluated.

RAD Studio does not have a notification system that informs the expression engine of any object or property change. In the sample applications, Notify is called from event handlers. Typically, the Sender parameter of the event handler is used to call BindingsList1.Notify(Sender, '');. In this way, you can use one event handler to handle notifications from many controls. The SynchControlsSampleProject sample application (that is also listed in the table below) exemplifies how the notifier is implemented.

TBindExpression and TBindExprItems are the LiveBindings components that support managed bindings. Set the Managed property. These two component also have another property that affects notifications. When the NotifyOutputs property is True, the expression engine will automatically send notification to an output object or property that is modified when a binding is evaluated.

None of the other binding components use managed bindings. They use unmanaged bindings, which are always explicitly evaluated.

The TBindLink, TBindListLink, and TBindGridLink components and the closely related FireMonkey TBindDB*Link components bind data such as a TDataSet field to controls such as TEdit. Rather than rely on the expression engines dependency graph to keep controls and data in sync, these components listen to notifications from the data source and notifications from the controls (using the new TObserver API). The system is a reorganization of the VCL DB aware control design, which does more than simply keep two properties in sync. In order for a control to work with these components and support data entry, it must implement the TObserver API. Read-only controls like TLabel do not need to support the TObserver API. Because TBindLink has been designed to work like the VCL DB aware controls, it mixes well with forms that use VCL DB aware controls, and has the benefit of supporting complex expressions and standard VCL controls.

The TBindPosition component is another component that listens to control notification using the TObserver API. This component is used with components that have a position like TListBox, Vcl.StdCtrls.TScrollBar, or TTracker. See the BindLookupVCLProject sample to see how TBindPosition can be used.

There are two other components that use unmanaged bindings. TBindList and TBindGridList are used to populate a list or grid. It is possible for two binding components to be used together on the same control. So, for example, a TBindList and a TBindDBList component can be used together to fill a TListBox that can edit a TDataSet field.

LiveBindings Samples

There are a number of LiveBindings sample applications shipped along with RAD Studio that you can find by clicking Start | Programs | Embarcadero RAD Studio XE2 | Samples. The folder of interest is Delphi\LiveBinding. These samples are listed in the following table, divided by their corresponding category (FMX or VCL). Some of the sample applications are available for both FMX and VCL while others are available for only one of these two categories. The main project group for all the LiveBindings samples is called BindSamplesGroup.groupproj.

Tip: For information on how to update the Samples directory to the latest SVN version, please refer to the Sample help topic.

FMX LiveBindings Samples VCL LiveBindings Samples
BindComponentsExprProject N/A
BindDBExprSampleProject N/A
BindExpressionSampleProject N/A
SynchControlsSampleProject N/A
BindGridLinkFMXProject BindGridLinkVCLProject
BindLinkFMXProject BindLinkVCLProject
BindLinkFMXProject BindLinkVCLProject
BindListFMXProject BindListVCLProject
N/A BindListEditVCLProject
N/A BindLookupVCLProject

These samples show you in detail how to build enhanced applications using LiveBindings.

The source for the LiveBindings samples in your product installation are checked into a public Subversion repository that can be found on SourceForge:

LiveBindings Visual Components

The LiveBindings engine is a run-time feature, but a number of visual components are present in the component palette, to help you create live bindings between components at design time.

If you browse the component palette, in the LiveBindings tab, there are the following components that you can drag and drop to a form (either VCL or FMX):

Tip: When you use the Object Inspector to create live bindings between components, the appropriate LiveBindings component (TBindingsList) is automatically placed on the form. This component holds a list of all bindings available in your Application. See the Bindings Lists topic for more information about bindings lists.

LiveBindings API Reference

The following units are part of the LiveBindings engine. The documentation in their appropriate help topics contains information regarding LiveBindings API usage.

Unit Name Contains
System.Bindings.CustomScope Custom scope related classes
System.Bindings.CustomWrapper Custom wrapper related classes
System.Bindings.EvalProtocol Interfaces used by the engine for the wrappers, scopes, and other internal structures
System.Bindings.Expression Binding expression related classes
System.Bindings.ExpressionDefaults Default behavior for binding expressions
System.Bindings.Factories Factories for expressions, managers, and scopes
System.Bindings.Helper Tools for programmatical creation of binding expressions and for notifications
System.Bindings.Manager Binding manager related implementations
System.Bindings.ManagerDefaults Default behavior for binding managers
System.Bindings.Methods Control of methods that can be used in the script
System.Bindings.Outputs Tools for control of expression outputs

Hint: When using C++Builder to develop console applications that make use of LiveBindings, you have to explicitly instruct the C++ compiler to generate RTTI information for the classes in your project. To do this, add the #pragma explicit_rtti methods() directive at the beginning of your project, as shown in the Using LiveBinding Programatically tutorial. For more information regarding the #pragma explicit_rtti methods() directive, please refer to the Pragma explicit rtti help topic.

See Also