FMX - TSTringGrid ColumnBackGround 奇怪的行为

问题描述 投票:0回答:1

我使用的是Delphi 12。

我想根据第 9 列中的颜色名称更改前 4 列的单元格背景(或者如果可能的话记录值,但我没能成功)。

procedure TDataGrid.OnDrawColumnBackground(Sender: TObject; const Canvas: TCanvas; const Column: TColumn;
  const Bounds: TRectF; const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var
  RowColor : TBrush;
begin
  if Column.Index < 4 then
  begin
    RowColor := Tbrush.Create(TBrushKind.Solid,TAlphaColors.Alpha);
    RowColor.Color := System.UIConsts.StringToAlphaColor(TStringGrid(Sender).Cells[9, TStringGrid(Sender).Row]);
    Canvas.FillRect(Bounds, 0, 0, [], 1, RowColor);
    TStringGrid(Sender).DefaultDrawing := true; 
  end;
end;

这种行为真的很奇怪:

当我打开表格时,我得到这个:

当我用键盘逐行向下时:

它正在用下一种颜色为线条着色

或者当我单击线条时,它会显示我单击的线条或鼠标位置的颜色!

最糟糕的是,当我向下滚动时,它使所有行再次以一种颜色着色,就像我刚刚再次打开表单一样。

有人知道如何让它发挥作用吗?

delphi firemonkey background-color tstringlist
1个回答
0
投票

使用

OnDrawColumnCell()
来绘制背景,然后调用
DefaultDrawCell
来绘制内容:

procedure TForm1.TDataGridDrawColumnCell(Sender: TObject;
  const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
  const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var
  RowColor : TBrush;
begin
  if Column.Index < 4 then
  begin
    RowColor := Tbrush.Create(TBrushKind.Solid,TAlphaColors.Alpha);
    RowColor.Color := System.UIConsts.StringToAlphaColor(TStringGrid(Sender).Cells[9, TStringGrid(Sender).Row]);
    Canvas.FillRect(Bounds, 0, 0, [], 1, RowColor);
  end;
  Column.DefaultDrawCell(Canvas,Bounds,Row,Value,State);
end;
© www.soinside.com 2019 - 2024. All rights reserved.