如何更改FMX.TGrid行的背景颜色取决于XE4中的值

时间:2014-02-28 12:35:44

标签: delphi firemonkey delphi-xe4 tgrid

我想知道如何在Firemonkey TGrid / TColumn中更改整行的背景颜色。 看到一堆类似的问题,但没有一个帮助我。我正在使用Delphi XE4。 TGrid可能包含TCheckColumnTStringColumn

1 个答案:

答案 0 :(得分:0)

TGrid行背景样式颜色分为两类:

  • 焦点颜色
  • 选择颜色

焦点颜色适用于聚焦细胞。选择颜色适用于选定的行。

更改焦点颜色是一个直接的过程:

procedure ChangeGridCellFocusColor(Grid: FMX.Grid.TGrid; NewColor: TAlphaColor);
var
  T: TFmxObject;
begin
  T := Grid.FindStyleResource('focus');
  if (T <> nil) and (T is TRectangle) then
    if TRectangle(T).Fill <> nil then
      TRectangle(T).Fill.Color := NewColor;
  Grid.Repaint;
end;

您可以这样申请:

ChangeGridCellFocusColor(MyGrid1, TAlphaColors.Red);

请注意,Focus矩形是半透明的,因此您指定的任何颜色都会与行选择颜色混合。


可以合理地假设选择颜色可以以相同的方式更改,但事实并非如此。

当应用样式时,将克隆标记为选择的资源,丢弃原始值并将新值添加到内部TControlList。 这就是为什么不能应用同样的原则。

要更改行选择颜色,请执行以下操作:

Interface

type
  TcustomGridHelper = class helper for FMX.Grid.TCustomGrid
  public
    function getSelections: TControlList;
  end;

{...}

Implementation

function TcustomGridHelper.getSelections: TControlList;
begin
  Result := Self.fSelections;
end;


procedure ChangeGridRowSelectionColor(Grid: FMX.Grid.TGrid;
  NewColor: TAlphaColor);
var
  aList: TControlList;
  Control: TControl;
begin
  aList := Grid.getSelections;
  if (aList <> nil) then
    for Control in aList do
      TRectangle(Control).Fill.Color := NewColor;
  Grid.Repaint;
end;

您可以这样申请:

ChangeGridRowSelectionColor(MyGrid1, TalphaColors.Green);
相关问题