繁体   English   中英

在运行时XE4更改TTextCell背景颜色

[英]Changing TTextCell background colour at runtime XE4

我以此处发布的示例为起点进行了研究: 在Firemonkey TGrid中更改TTextCell的背景

我创建了一个引用图像的textcellstyle,它运行良好。 当我运行程序时,所有单元格都按预期显示背景图像。

通过上面的链接,Mike Sutton(我希望您正在阅读,如果没有您的输入,我们会做什么!)写道(在此处重复以简化操作):

“然后,您可以将每个单元格的StyleLookup属性设置为使用它,或将StyleName设置为TextCellStyle以使每个TTextCell自动选择它。”

从有关更改字体颜色的查询( Delphi XE4 Firemonkey网格控件-分别设置样式 )开始,可以动态设置背景颜色吗?

我创建单元格的代码:

Constructor TFinancialCell.Create(AOwner:TComponent);

begin
  inherited;
  StyleLookup:='textcellstyle';
  StyledSettings:=StyledSettings-[TStyledSetting.ssStyle,TStyledSetting.ssFontColor]; 
  TextAlign:=TTextAlign.taTrailing;
end;

这将我的图像成功地应用于TFinancialCell。

但是,根据字体颜色查询,我希望图像背景仅在达到某个值或任何其他值时显示:

Procedure TFinancialCell.ApplyStyling;
begin
  Font.Style:=[TFontStyle.fsItalic];

  If IsNegative then
    FontColor:=claRed
  else
    FontColor:=claGreen;

  If IsImportant then Font.Style:=[TFontStyle.fsItalic,TFontStyle.fsBold]; 
  If Assigned(Font.OnChanged) then
    Font.OnChanged(Font);

  Repaint;
end;

任何有关如何做到这一点的帮助将不胜感激。

谢谢迈克。 我不得不摆弄一些,但是根据您的建议使它起作用。 我在stylecontainer的textcellstyle中添加了一个TRectangle,如下所示:

textcellstyle : TLayout
    background: TSubImage
        rectangle1: TRectangle
        rectanimation: TRectAnimation

在TFinancialCell.ApplyStyle中,我尝试了FindStyleResource(“背景”),但是始终返回nil。 我将其更改为FindStyleResource('rectangle1'),效果很好。 这是因为它在对象检查器中寻找相关的StyleName属性(对于矩形1,它显然默认为'Rectangle1')? 我仍然可以肯定地说,仍然还不太能看到树木的树木。

工作代码:

Procedure TFinancialCell.ApplyStyle;

var 
  T : TFMXObject;

begin
  inherited;

  T:=FindStyleResource('Rectangle1');

  If (T<>nil) and (T is TRectangle) then
  begin 
    If TRectangle(T).Fill<>nil then 
    begin 
      If IsNegative then 
      begin
        TRectangle(T).Fill.Color:=claRed; 
        Repaint;
      end;  
    end;
  end;

  ApplyStyling;
end;

作为单独的练习,我也尝试将代码放在TFinancialCell.ApplyStyling上,它也在那里工作,所以不确定哪个是更好的选择,为什么?

到目前为止,我对这些样式的理解摘要如下(请根据需要进行纠正/添加注释):

  1. 我创建了一种称为textcellstyle的样式,该样式已在TFinancialCell.Create中应用到我的TFinancialCell类[StyleLookup:='textcellstyle']。
  2. 当我调用TFinancialCell.ApplyStyling时,我能够直接访问TFinancialCell的Font和FontColor属性,因为这些属性是TTextCell的属性。
  3. 如果要绘制单元格的背景,则必须显式调用手动添加到textcellstyle'style'的TRectangle组件,然后从那里访问Fill等属性。

暂无
暂无

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

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