简体   繁体   English

在运行时将样式应用于Delphi XE4 Firemonkey StringGrid单元

[英]Applying a style to a Delphi XE4 Firemonkey StringGrid cell at runtime

I am trying to apply a style to an XE4 FM Stringgrid at runtime, but am unable to find the correct syntax to do this. 我正在尝试在运行时将样式应用于XE4 FM Stringgrid,但是无法找到正确的语法来执行此操作。

The StringGrid already inherits the 'TextCellStyle' (the default) which I have created at design time, and display the cells in the stringgrid according to this style. StringGrid已经继承了我在设计时创建的“ TextCellStyle”(默认值),并根据此样式在stringgrid中显示单元格。

What I would ideally like to do is change the colour of the fonts in specific cells (negative=Red, positive=green etc) at runtime, but cannot work out how to do this, as I am unable to access the Stylelookup at cell level. 我理想上想做的是在运行时更改特定单元格(负=红色,正=绿色等)中字体的颜色,但无法解决该问题,因为我无法在单元级别访问Stylelookup 。

Please remember that this query relates to a TStringGrid, and not a TGrid, as our application requires us to allocate memory to the grid dynamically at run-time, and it is much easier to do with a stringgrid. 请记住,该查询与TStringGrid有关,而不与TGrid有关,因为我们的应用程序要求我们在运行时动态地将内存分配给网格,而使用stringgrid则容易得多。

Any help would be very much appreciated, and thanks in advance. 任何帮助将不胜感激,并在此先感谢。

I have been attempting to learn how to do this with a TGrid, and thanks to Mike Sutton's help have managed this. 我一直在尝试学习如何使用TGrid来做到这一点,并且在Mike Sutton的帮助下,这一点得到了解决。

[See Changing TTextCell background colour at runtime XE4 for background.] [请参阅在运行时XE4更改TTextCell背景颜色作为背景。]

Having managed to get this right, I attempted a similar logic on a TStringGrid and it worked fine. 设法做到这一点后,我在TStringGrid上尝试了类似的逻辑,并且工作正常。 As stringgrid doesn't use Grid1GetValue, I simply hard-coded random numbers into the grid in FormCreate. 由于stringgrid不使用Grid1GetValue,因此我只是将随机数硬编码到FormCreate中的网格中。

[In the code below I have a style called textcellstyle; [在下面的代码中,我有一种称为textcellstyle的样式; to the 'background' component of textcellstyle I added a TRectangle, so I could call on the 'Fill' property of TRectangle. 我在textcellstyle的'background'组件中添加了一个TRectangle,因此可以调用TRectangle的'Fill'属性。 Still not on top of the styles yet, see link above.] 仍未在样式之上,请参阅上面的链接。]

My code, in case it's helpful: 我的代码,如果有帮助的话:

    Procedure TForm1.FormCreate(Sender : TObject);

    begin
      { CREATE AN EXTRA COLUMN }
      StringGrid1.AddObject(TFinancialColumn.CreateStringGrid1));  
      { HARD-CODE THE ROWS }
      StringGrid1.Cells[0,0] :='0';
      StringGrid1.Cells[0,1] :='1';
      StringGrid1.Cells[0,2] :='2';
      StringGrid1.Cells[0,3] :='3';
      StringGrid1.Cells[0,4] :='4';
      StringGrid1.Cells[0,5] :='5';
      StringGrid1.Cells[0,6] :='6';
      StringGrid1.Cells[0,7] :='7';
      StringGrid1.Cells[0,8] :='8';
      StringGrid1.Cells[0,9] :='9';
      StringGrid1.Cells[0,10]:='10';
      { HARD-CODE A BUNCH OF NUMBERS. NOTE THAT HASH IN FRONT OF A NUMBER IS SIMPLY A  FLAG FOR IsImportant }
      StringGrid1.Cells[1,0] :='-10';
      StringGrid1.Cells[1,1] :='-6.86999999';
      StringGrid1.Cells[1,2] :='76.0999999';
      StringGrid1.Cells[1,3] :='#10.25';        
      StringGrid1.Cells[1,4] :='#17.2900006';
      StringGrid1.Cells[1,5] :='#57.1599993';
      StringGrid1.Cells[1,6] :='21.86000';
      StringGrid1.Cells[1,7] :='6.17';
      StringGrid1.Cells[1,8] :='27.219999';
      StringGrid1.Cells[1,9] :='#32.56000';
      StringGrid1.Cells[1,10]:='-1.7999';
    end;


    Function TFinancialColumn.CreateCellControl : TStyledControl;

    begin
      Result:=TFinancialCell.Create(Self);

      TTextCell(Result).OnTyping:=DoTextChanged;
      TTextCell(Result).OnExit  :=DoTextExit;
    end;

    Constructor TFinancialCell.Create(AOwner : TComponent);

    begin
      inherited;
      StyleLookup:='textcellstyle';
      StyledSettings:=StyledSettings-[TStyledSetting.ssStyle,TStyledSetting.ssFontColor]; { THIS LINE MUST BE HERE TO APPLY A NEW STYLE; IT CLEARS THE 'DEFAULT' STYLE SETTINGS }
      TextAlign:=TTextAlign.taTrailing;
    end;

    Procedure TFinancialCell.SetData(const Value          : TValue);

    var 
      F                                                   : Single;
      O                                                   : TFMXObject;
      S                                                   : String;

    begin
      S:=Value.AsString;

      If Length(S)>1 then 
      begin 
        FIsImportant:=S[1]='#';
        If IsImportant then
          S:=Copy(Value.AsString,2,MaxInt)
        else
          S:=Value.AsString;

        F:=StrToFloat(S);
        inherited SetData(Format('%n',[F]));
        FIsNegative:=F<0;

        ApplyStyling;
      end;  
    end;

    Procedure TFinancialCell.ApplyStyle;

    var 
      T                                                   : TFMXObject;

    begin
      inherited;

      T:=FindStyleResource('rectangle1');

      If T is TRectangle then
      begin 
        If IsNegative then 
        begin
          TRectangle(T).Fill.Color:=claRed; 
        end;  
      end;

      ApplyStyling;
    end;

    Procedure TFinancialCell.ApplyStyling;

    var 
      T                                                   : TFMXObject;

    begin
      If IsNegative then
        FontColor:=claBlack
      else
        FontColor:=claGreen;

      If IsImportant then Font.Style:=[TFontStyle.fsItalic,TFontStyle.fsBold]; { REPEAT THE ITALIC ELSE IT WILL ONLY BE BOLD, IE IT OVERWRITES THE ITALIC WITH BOLD }

      If Assigned(Font.OnChanged) then
        Font.OnChanged(Font);

      Repaint;
    end;

This code works in terms of restyling the fonts and backgrounds. 此代码可以重新设置字体和背景的样式。 Any suggestions to improve the above are most welcome, but hopefully this can help. 欢迎提出任何改善上述建议的建议,但希望能有所帮助。

The styling falls apart as soon as you start scrolling, though, haven't figured out how to fix that yet. 但是,一旦开始滚动,样式就会瓦解,但还没有找到解决方法。 Any suggestion would be most welcome! 任何建议将是最欢迎的!

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

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