12

How can I customize my listview to display different background colors like in the picture below ?

Items with different background color

My listview is bound to a datasource (Livebindng). I want to use the color field to set my backgroud color.

I've customized my view this way :

  • 3 Text items (Designation,Date and Resume)
  • 1 Bitmap item (Couleur)

Text items are bound to datasource but there is no way to bind my Bitmap to my "color" field.

enter image description here

I've filled the listview ActivesUpdateObjects event but this is not enough as bitmap is not changed when datasource record is updated!

procedure TfrmMain.lvTachesActivesUpdateObjects(const Sender: TObject;
  const AItem: TListViewItem);
begin
  SetItemColor(AItem);

end;

procedure TfrmMain.SetItemColor(const AItem: TListViewItem; const UpdateColor:
    Boolean = False);
var
  LObject: TListItemImage;
  VC: TColor;
begin
  LObject        := AItem.Objects.FindObjectT<TListItemImage>('Couleur');
  VC:= dtmMain.qrTaches.FieldByName('couleur').AsInteger;
  if LObject.Bitmap = nil then
  begin

  LObject.Bitmap := FMX.Graphics.TBitmap.Create(10,240);
  LObject.Bitmap.Clear(VC);
  end else if UpdateColor then LObject.Bitmap.Clear(VC);

end;

Is there a better way to proceed? I was also looking to use style but it appears (or I didn't find) that itemlistview can apply styles!

Ps : Firemonkey / Windows / Delphi Berlin XE10.1

Shaun Roselt
  • 1,650
  • 5
  • 18
  • 44

1 Answers1

0

I'm using Delphi 7 so take this with a grain of salt.

You may have to write your own CustomDrawItem method on your TreeView to handle this stuff

This is mine (I edited out some code because it has some lengthy logic behind). Also, I don't draw icons so the DrawImage part is commented.

procedure TVentanaVisorComponentes.TreeView1CustomDrawItem(
  Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState;
  var DefaultDraw: Boolean);
var
  NodeRect: TRect;
  EsSeleccion, EsDespejado: boolean;
begin
  with TreeView1.Canvas do
  begin
    //If DefaultDraw it is true, any of the node's font properties can be
    //changed. Note also that when DefaultDraw = True, Windows draws the
    //buttons and ignores our font background colors, using instead the
    //TreeView's Color property.

    DefaultDraw := False;
    //DefaultDraw = False means you have to handle all the item drawing yourself,
    //including the buttons, lines, images, and text.
    if not DefaultDraw then
    begin
      Brush.Color := clMenuHighLight;
      Font.Color := clWhite;
      NodeRect := Node.DisplayRect(True);
      FillRect(NodeRect);
      // ...
      NodeRect := Node.DisplayRect(False);
      // ...
      FillRect(NodeRect);

      NodeRect.Left := NodeRect.Left + (Node.Level * TreeView1.Indent);
      //NodeRect.Left now represents the left-most portion of the expand button
      DrawButton(NodeRect, Node);

      NodeRect.Left := NodeRect.Left + TreeView1.Indent;
      //NodeRect.Left is now the leftmost portion of the image.
      //DrawImage(NodeRect, Node.ImageIndex);

      // NodeRect.Left := NodeRect.Left + ImageList.Width;
      //Now we are finally in a position to draw the text.

      TextOut(NodeRect.Left, NodeRect.Top, (Node as TNodoArbolComponentes).Texto);
    end;
  end;
end;
Héctor C.
  • 433
  • 4
  • 17