如何更改TListview项目的背景颜色?

Ste*_*cki 11 delphi firemonkey

如何自定义列表视图以显示不同的背景颜色,如下图所示?

具有不同背景颜色的项目

我的listview绑定到数据源(Livebindng).我想使用颜色字段来设置我的背景颜色.

我这样定制了我的观点:

  • 3个文本项目(指定,日期和简历)
  • 1位图项目(Couleur)

文本项绑定到数据源,但无法将我的位图绑定到"颜色"字段.

在此输入图像描述

我已经填充了listview ActivesUpdateObjects事件,但这并不是因为当数据源记录更新时位图没有改变!

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;
Run Code Online (Sandbox Code Playgroud)

还有更好的方法吗?我也在寻找o使用样式但是看起来(或者我没有找到)itemlistview无法应用样式!

Ps:Firemonkey/Windows/Delphi Berlin XE10.1

Héc*_* C. 0

我使用的是 Delphi 7,所以对此持保留态度。

您可能必须在 TreeView 上编写自己的 CustomDrawItem 方法来处理这些东西

这是我的(我编辑了一些代码,因为它背后有一些冗长的逻辑)。另外,我不绘制图标,因此对 DrawImage 部分进行了注释。

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;
Run Code Online (Sandbox Code Playgroud)