ITDevCon 9: ListView and LiveBindings

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:

  1. show an image (glyph) according to some data of the item;
  2. 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
LTextObject: TListItemText;
LItem: TListViewItem;
begin
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
else
LTextObject.TextColor := TAlphaColorRec.Black;
end;

Hope this can be useful.

Andrea

6 thoughts on “ITDevCon 9: ListView and LiveBindings

  1. Paolo Brovelli says:

    The “demo effect” hit the bests 😉 Do u remember “uncle” Bill Gates?

    However, good article 🙂

    1. Andrea Magni says:

      A classic: https://www.youtube.com/watch?v=IW7Rqwwth84

      I will try to consolate myself at dinner 😉

  2. setiawan nugroho says:

    why thr data does not occur on the listview ?

  3. setiawan nugroho says:

    why the data does not occur on the listview ?
    text1,text2,image3 was not found . I use delphi xe8

    1. You should add the MultipleListView.dcu, you can found on sample project delphi folder

  4. Ahmed Aymen says:

    hi
    how to format date like 2020/05/12 to be like 12-05-2020 using CustomFormat

Leave a Reply to Paolo Brovelli Cancel reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.