Using Custom Format and Parse Expressions in LiveBindings
Go Up to LiveBindings in RAD Studio
When you bind controls, properties or fields using LiveBindings, you might want to modify the data in one end of a binding before this data reaches the other end of the binding. For example, if you have a dataset with a list of capitalized color names, you might want to convert them to lowercase before you show them in a list. Often these changes go both ways; for example, if you have a dataset with ISO dates, you might want to convert them to the local date format when you display them to your user, and you also want to convert any date manually entered by your user into ISO format before you store it in your dataset.
When you bind controls, properties or fields using LiveBindings, you are creating an instance of a binding class such as TLinkFillControlToField or TLinkFillControlToProperty. The properties of these classes include the following two types of properties:
CustomFormat
properties, such asFillBreakCustomFormat
orFillDisplayCustomFormat
. In these properties you can define an expression that modifies the value from the source control, property or field.CustomParse
properties, such asFillBreakCustomParse
orFillDisplayCustomParse
. In these properties you can define an expression that modifies the input value before it is stored into the source control, property or field.
Contents
Specifying a Custom Format or Parse Expression
To specify a custom format or parse expression in an existing binding between controls, fields, or properties:
- Open the LiveBindings Designer and select the target binding.
- In the Object Inspector:
- To specify a custom format expression, locate the matching
CustomFormat
property value (for example,FillHeaderCustomFormat
orFillValueCustomFormat
), enter a binding expression, such asLowerCase(%s)
, and press Enter to save this new property value.
- Adding this expression ensures that values from the source control, property, or field are affected by the specified expression before they are passed to the target control property or field.
- To specify a custom parse expression, locate the matching
CustomParse
property (for example,FillHeaderCustomParse
orFillValueCustomParse
), and enter a binding expression, such asUpperCase(%s)
, and press Enter to save this new property value.
- Now input values are affected by the specified expression before they are stored into the source control, property, or field.
- To specify a custom format expression, locate the matching
Binding Expressions
The simplest binding expression that you can use is %s
, where %s
stands for the source value. That is, if you simply use %s
in a custom format property of a binding, the source value does not change; the result is the same that you get if you leave the custom format property empty.
Accessing the Source Object
You can also use Self.Owner
to access the source object; this gives you access not only to the data associated with the binding, but to all data from the source object. You can use any of the following expressions to access a piece of data from the source object:
Self.Owner.Field.Property
. For example:Self.Owner.CUSTOMER.Text
.- Note: You can use this syntax as long as the source object name ("CUSTOMER" in the previous example) is a valid ID. If the source object is a field, its name might not be a valid name; in this case, use the syntax below instead:
Self.Owner.FieldByName('Field').Property
.- For example:
Self.Owner.FieldByName('CUSTOMER').Text
.
- For example:
See Video: Using binding expressions in the CustomFormat property of a TLinkPropertyToField component.
Using Math
You can use the following math symbols in your expressions:
- Constants:
nil True False Pi
- Arithmetic operators:
+ - * /
- Logic operators:
= <> < <= > >=
- Parentheses,
()
, to change operator precedence.
Concatenating Strings
To add a prefix or a suffix to the source value, surround this additional string with single quotes ( ' ) and use a plus sign (+) to concatenate your string to the source string. For example, 'Hello, ' + %s + '!'
.
Using LiveBindings Methods
LiveBindings methods are methods that you can use in your custom binding expressions. For example, you could write an expression such as 'I like ' + LowerCase(%s) + ' crayons'
, where the source string, %s
, would be converted to lowercase characters.
- Note: LiveBindings methods are case-sensitive. If the LiveBindings method name is
LowerCase
, using eitherLowercase
orlowercase
does not work.
RAD Studio provides many default LiveBindings methods that you can use, such as LowerCase
, UpperCase
or Format
. For a list of these methods and documentation about them, see Default LiveBindings Methods
You can also create your own LiveBindings methods. See Creating Custom LiveBindings Methods.
When you bind controls, properties, or fields using the LiveBindings Designer, a TBindingsList component is added to your form. You can open the Methods
property of this component to see a list of available LiveBindings methods that you can use in your custom binding expressions, both those provided by RAD Studio and your own, custom LiveBindings methods.
Performance
When LiveBindings populates list-based or table-based controls, such as list views, grids or combo boxes, using custom format or parse expressions can have a noticeable negative effect on performance, as it prevents evaluation shortcuts from working.
See Also
- Mobile Tutorial: Using LiveBindings to Populate a ListBox in Mobile Applications (iOS and Android)
- Video: Using binding expressions in the CustomFormat property of a TLinkPropertyToField component
- A quick guide to evaluate and compile expressions using the LiveBindings expression evaluator
- LiveBindings in XE3: Formatting your Fields
- LiveBindings : How to treat an Integer field as a Boolean?