Today, during my session at ITDevCon 9, I have been hit by the typical “demo effect” and my example about TListView customization and LiveBinding’s CustomFormat functionality refused to work.
Obviously after a good coffee (we are in Italy, right?) and 5 minutes to rebuild my example from scratch, everything works. Who knows what happened.
So I am sharing here my example about two typical functionalities around listviews and databindings:
- show an image (glyph) according to some data of the item;
- formatting a number as money.
Both are implemented through the use of CustomFormat property. We have a dataset (the “orders” dataset you can find in your Samples\Data folder of RAD Studio/Delphi installation) and a TListView bound to it through LiveBindings technology. The TListView has a dynamic appearance and three drawables in each item: Text1, Text2 (two TTextObjectAppearance objects) and Image3 (a TImageObjectAppearance object). There is a TImageList (linked to the TListView through its Images property) containing a couple of images.
- Text1 shows the order number with a fixed ‘Order No.’ prefix.
CustomFormat: ‘Order No. ‘ + AsString
- Text2 shows the ItemsTotal field value, properly formatted as money.
CustomFormat: Format(‘%%m’, Owner.ItemsTotale.AsFloat + 0.0)
(the ‘+ 0.0’ is a trick to force the expression to match the required type)
- Image3 shows a different image whether ItemsTotal is greater/lower than 30k.
CustomFormat: IfThen(Owner.ItemsTotal.AsFloat > 30000, 0, 1)
The following picture summarize the scenario and the result (directly in the IDE).
Hope everybody who was attending can find this useful. The complete example is available here.
LiveBindings may still need some love but can be very effective in a number of situations and are actually a very powerful mechanism.
Update: someone (Thomas) asked about how to change the TextColor of the drawable showing the ItemsTotal in the ListView depending on some conditions. You can use the OnFillingListItem event of the LiveBinding object (LinkFillControlToField1) and write something like this:
procedure TForm1.LinkFillControlToField1FillingListItem(Sender: TObject; const AEditor: IBindListEditorItem); var
LItem := AEditor.CurrentObject as TListViewItem;
LTextObject := LItem.Objects.FindDrawable('Text2') as TListItemText;
if Assigned(LTextObject) then
if FDMemTable1.FieldByName('ItemsTotal').AsCurrency > 30000 then
LTextObject.TextColor := TAlphaColorRec.Firebrick
LTextObject.TextColor := TAlphaColorRec.Black;
Hope this can be useful.