如何更改FMX.TGrid行的背景颜色取决于XE4中的值 [英] How to change background color of FMX.TGrid row depend on value in XE4

查看:149
本文介绍了如何更改FMX.TGrid行的背景颜色取决于XE4中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to know how to change background color of whole row in Firemonkey TGrid/TColumn. Saw bunch of similar questions, but none of them helped me. I'm using Delphi XE4. TGrid may contain TCheckColumn and TStringColumn.

推荐答案

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

TGrid row background style color is divided into two categories :


  • 焦点颜色

  • 选择颜色

焦点颜色适用于聚焦的单元格。选择颜色适用于选定的行。

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;

您可以这样应用:

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.

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

It would be reasonable to assume that the Selection color can be changed in the same fashion but that is not the case.

应用样式时,将克隆标记为选择的资源,丢弃原始值,并将新值添加到内部< a href = http://docwiki.embarcadero.com/Libraries/XE5/en/FMX.Controls.TControlList rel = nofollow> TControlList 。
这就是为什么不能应用相同原理的原因。

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

您可以这样应用:

ChangeGridRowSelectionColor(MyGrid1, TalphaColors.Green);

这篇关于如何更改FMX.TGrid行的背景颜色取决于XE4中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆