简体   繁体   English

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

[英]Changing TTextCell background colour at runtime XE4

I worked through the example posted here as my starting point: Change background of TTextCell in a Firemonkey TGrid 我以此处发布的示例为起点进行了研究: 在Firemonkey TGrid中更改TTextCell的背景

I have created a textcellstyle which references an image, and this is working well. 我创建了一个引用图像的textcellstyle,它运行良好。 When I run the program all the cells display the background image as expected. 当我运行程序时,所有单元格都按预期显示背景图像。

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

"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." “然后,您可以将每个单元格的StyleLookup属性设置为使用它,或将StyleName设置为TextCellStyle以使每个TTextCell自动选择它。”

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

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;

This applies my image successfully to TFinancialCell. 这将我的图像成功地应用于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. 任何有关如何做到这一点的帮助将不胜感激。

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

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

In TFinancialCell.ApplyStyle I tried FindStyleResource ('background'), but this always returned nil. 在TFinancialCell.ApplyStyle中,我尝试了FindStyleResource(“背景”),但是始终返回nil。 I changed it to FindStyleResource ('rectangle1') and this worked great. 我将其更改为FindStyleResource('rectangle1'),效果很好。 Is this because it looks for the relevant StyleName property (which obviously defaults to 'Rectangle1' for rectangle 1) in the object inspector? 这是因为它在对象检查器中寻找相关的StyleName属性(对于矩形1,它显然默认为'Rectangle1')? Still not quite seeing the wood for the trees, as I'm sure you can tell... 我仍然可以肯定地说,仍然还不太能看到树木的树木。

The working code: 工作代码:

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;

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

The summary of my understanding of these styles so far is (please correct/comment as necessary): 到目前为止,我对这些样式的理解摘要如下(请根据需要进行纠正/添加注释):

  1. I have created a style called textcellstyle, which I apply in TFinancialCell.Create to my TFinancialCell class [StyleLookup:='textcellstyle']. 我创建了一种称为textcellstyle的样式,该样式已在TFinancialCell.Create中应用到我的TFinancialCell类[StyleLookup:='textcellstyle']。
  2. When I call TFinancialCell.ApplyStyling, I am able to access the Font and FontColor properties of TFinancialCell directly, as these properties are properties of TTextCell. 当我调用TFinancialCell.ApplyStyling时,我能够直接访问TFinancialCell的Font和FontColor属性,因为这些属性是TTextCell的属性。
  3. If I want to paint the background of the cells, I have to explicitly call the TRectangle component that I manually added to the textcellstyle 'style', and then access the Fill etc property from there. 如果要绘制单元格的背景,则必须显式调用手动添加到textcellstyle'style'的TRectangle组件,然后从那里访问Fill等属性。

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

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