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

时间:2013-05-14 14:06:50

标签: delphi fonts colors firemonkey stringgrid

我正在尝试在运行时将样式应用于XE4 FM Stringgrid,但无法找到正确的语法来执行此操作。

StringGrid已经继承了我在设计时创建的'TextCellStyle'(默认值),并根据此样式显示stringgrid中的单元格。

我理想的做法是在运行时更改特定单元格中的字体颜色(负数=红色,正数=绿色等),但无法解决如何执行此操作,因为我无法访问Stylelookup在细胞水平。

请记住,此查询与TStringGrid有关,而不是TGrid,因为我们的应用程序要求我们在运行时动态地为网格分配内存,并且使用stringgrid更容易。

非常感谢任何帮助,并提前感谢。

1 个答案:

答案 0 :(得分:0)

我一直试图通过TGrid学习如何做到这一点,并且感谢Mike Sutton的帮助已经成功实现了这一目标。

[有关背景,请参阅Changing TTextCell background colour at runtime XE4。]

设法做到这一点,我在TStringGrid上尝试了类似的逻辑,它工作正常。由于stringgrid不使用Grid1GetValue,我只是在FormCreate中将随机数硬编码到网格中。

[在下面的代码中我有一种叫做textcellstyle的风格;到textcellstyle的'background'组件我添加了一个TRectangle,所以我可以调用TRectangle的'Fill'属性。尚未在样式之上,请参阅上面的链接。]

我的代码,万一它有用:

    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;

此代码适用于重新设置字体和背景。我们非常欢迎任何有关改进上述内容的建议,但希望这可以提供帮助。

一旦开始滚动,样式就会崩溃,但是,还没有想出如何解决这个问题。任何建议都是最受欢迎的!

相关问题