且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-06 15:14:40

谢谢 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 等属性.