在运行时更改 TTextCell 背景颜色 XE4 [英] Changing TTextCell background colour at runtime XE4

查看:17
本文介绍了在运行时更改 TTextCell 背景颜色 XE4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此处发布的示例作为我的起点:更改Firemonkey TGrid 中 TTextCell 的背景

I worked through the example posted here as my starting point: Change background of TTextCell in a Firemonkey TGrid

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

I have created a textcellstyle which references an image, and this is working well. When I run the program all the cells display the background image as expected.

从上面的链接中,Mike Sutton(我希望你正在阅读这篇文章,如果没有你的意见,我们会怎么做!)写道(在这里重复只是为了更容易):

From the above link, Mike Sutton (I hope you're reading this, what would we do without your input!) writes (repeated here just to make it easier):

然后,您可以设置每个单元格的 StyleLookup 属性以使用它,或者将样式 StyleName 设置为 TextCellStyle 以便为每个 TTextCell 自动拾取它."

"You can then set each of your cells StyleLookup properties to use it, or set the styles StyleName to TextCellStyle to have it picked up automatically for every TTextCell."

接着关于更改字体颜色的查询 (Delphi XE4Firemonkey Grid Control - 单独设置单元格样式),也可以动态设置背景颜色吗?

Following on from the query about changing the font colours (Delphi XE4 Firemonkey Grid Control - Styling cells individually), can one set the background colours dynamically as well?

我创建单元格的代码:

Constructor TFinancialCell.Create(AOwner:TComponent);

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

这会将我的图像成功应用到 TFinancialCell.

This applies my image successfully to TFinancialCell.

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

But, as per the font colour query, I would like the image background to display only when a certain value is reached or whatever:

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;

任何有关如何执行此操作的帮助将不胜感激.

Any help as to how to do this would be appreciated.

推荐答案

谢谢 Mike.我不得不摆弄一下,但根据你的建议让它工作.我在stylecontainer中给我的textcellstyle添加了一个TRectangle,如下:

Thanks Mike. I had to fiddle around a bit, but got it to work based on your suggestion. I added a TRectangle to my textcellstyle in the stylecontainer, as follows:

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

在 TFinancialCell.ApplyStyle 中,我尝试了 FindStyleResource ('background'),但这总是返回 nil.我将其更改为 FindStyleResource ('rectangle1'),效果很好.这是因为它在对象检查器中查找相关的 StyleName 属性(对于矩形 1 显然默认为Rectangle1")?仍然没有完全看到树木的树木,我相信你可以告诉......

In TFinancialCell.ApplyStyle I tried FindStyleResource ('background'), but this always returned nil. I changed it to FindStyleResource ('rectangle1') and this worked great. Is this because it looks for the relevant StyleName property (which obviously defaults to 'Rectangle1' for rectangle 1) in the object inspector? Still not quite seeing the wood for the trees, as I'm sure you can tell...

工作代码:

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 中,它也在那里工作,所以不确定哪个是更好的选择,为什么?

I also tried, as a separate exercise, to put the code above in TFinancialCell.ApplyStyling, and it also worked there, so not sure which is the better option, and why?

到目前为止我对这些风格的理解总结是(请根据需要更正/评论):

The summary of my understanding of these styles so far is (please correct/comment as necessary):

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

这篇关于在运行时更改 TTextCell 背景颜色 XE4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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