繁体   English   中英

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

[英]How to change background color of FMX.TGrid row depend on value in XE4

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

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM