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

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

问题描述

我通过此处发布的示例作为我的出发点:更改背景的TTextCell在Firemonkey TGrid

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 XE4 Firemonkey网格控制 - 单独设置单元格),可以动态设置背景颜色吗?

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?

我的创建单元格的代码:

My code on creating the cells:

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中添加了一个TRectangle到我的textcellstyle,如下:

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 I尝试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 etc属性。

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

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