且构网

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

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

更新时间:2023-12-06 15:05:58

感谢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属性。