简体   繁体   English

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

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

I'd like to know how to change background color of whole row in Firemonkey TGrid / TColumn . 我想知道如何在Firemonkey TGrid / TColumn更改整行的背景颜色。 Saw bunch of similar questions, but none of them helped me. 看到一堆类似的问题,但没有一个帮助我。 I'm using Delphi XE4. 我正在使用Delphi XE4。 TGrid may contain TCheckColumn and TStringColumn . TGrid可能包含TCheckColumnTStringColumn

TGrid row background style color is divided into two categories : TGrid行背景样式颜色分为两类:

  • Focus color 聚焦色彩
  • Selection color 选择颜色

Focus color applies to the focused Cell. 焦点颜色适用于聚焦细胞。 Selection color applies to the selected Row. 选择颜色适用于选定的行。

Changing focus color is a straight forward process: 改变焦点颜色是一个直接的过程:

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;

You apply it like this : 你像这样申请:

ChangeGridCellFocusColor(MyGrid1, TAlphaColors.Red);

Note that the Focus rectangle is semi-transparent so whatever color you assign it will mix with the Row Selection color. 请注意,Focus矩形是半透明的,因此您指定的任何颜色都将与“行选择”颜色混合。


It would be reasonable to assume that the Selection color can be changed in the same fashion but that is not the case. 假设选择颜色可以以相同的方式改变,但事实并非如此,这是合理的。

When the Style is Applied the resource marked as selection is cloned, the original value discarded and the new value added to an internal TControlList . 应用样式时,将克隆标记为选择的资源,丢弃原始值并将新值添加到内部TControlList This is why the same principle cannot be applied. 这就是为什么不能应用相同的原理。

To change the row selection color do the following: 要更改行选择颜色,请执行以下操作:

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;

You apply it like this : 你像这样申请:

ChangeGridRowSelectionColor(MyGrid1, TalphaColors.Green);

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

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