Changing TTextCell background colour at runtime XE4

前端 未结 1 877
野的像风
野的像风 2021-01-26 12:46

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

I have created a textcellstyle which references an im

相关标签:
1条回答
  • 2021-01-26 13:00

    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
    

    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...

    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?

    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'].
    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.
    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.
    0 讨论(0)
提交回复
热议问题