Using Custom Format and Parse Expressions in LiveBindings

From RAD Studio
Jump to: navigation, search

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:

Specifying a Custom Format or Parse Expression

To specify a custom format or parse expression in an existing binding between controls, fields, or properties:

  1. Open the LiveBindings Designer and select the target binding.
    The LiveBindings Designer shows bindings as blue arrows that connect two fields. When you click an arrow to select it, it becomes thicker, as shown in these images:
    LiveBindingsDesignerUnselectedBinding.png LiveBindingsDesignerSelectedBinding.png
  2. In the Object Inspector:
    • To specify a custom format expression, locate the matching CustomFormat property value (for example, FillHeaderCustomFormat or FillValueCustomFormat), enter a binding expression, such as LowerCase(%s), and press Enter to save this new property value.
      ObjectInspectorCustomFormatLowerCase.png
    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.
    FormDesignerCapitalizedColorNameList.png FormDesignerLowerCaseColorNameList.png
    • To specify a custom parse expression, locate the matching CustomParse property (for example, FillHeaderCustomParse or FillValueCustomParse), and enter a binding expression, such as UpperCase(%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.

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.

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 either Lowercase or lowercase 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