Firemonkey Grid Control - Styling a Cell based on a value (via the OnGetValue function call)

后端 未结 2 982
我在风中等你
我在风中等你 2020-12-03 22:56

I am looking for recommended solution to style a TGrid cell that is being drawn by the OnGetValue call (that is called to paint the cells in view). For background, an excel

相关标签:
2条回答
  • 2020-12-03 23:00

    Firstly, an apology. In my answer to your last question, CreateCellControl should have called inherited to create the cell. I've amended my answer.

    As for this question, I've uploaded my blog posting on FireMonkey Cells - http://monkeystyler.com/blog/entry/firemonkey-grid-basics-custom-cells-and-columns - it covers the stuff from the previous answer, and also covers creating custom cell controls. You'll need to read that before your proceed. I'll wait.

    ...

    Back now? Good.

    Following on from the example in the blog post.

    Except, that I've updated the TFinancialCell to inherit directly from TTextCell (which of course is a TEdit), which makes far more sense and is far simpler to style.

    So, update the TFinancialCell:

    type TFinancialCell = class(TTextCell)
      private
        FIsNegative: Boolean;
        FIsImportant: Boolean;
      protected
        procedure SetData(const Value: Variant); override;
        procedure ApplyStyle;override;
        procedure ApplyStyling;
      public
        constructor Create(AOwner: TComponent); override;
      published
        property IsNegative: Boolean read FIsNegative;
        property IsImportant: Boolean read FIsImportant;
      end;
    

    Code for the above:

    procedure TFinancialCell.ApplyStyle;
    var T: TFMXObject;
    begin
      inherited;
      ApplyStyling;
    end;
    
    procedure TFinancialCell.ApplyStyling;
    begin
      if IsNegative then
        FontFill.Color := claRed
      else
        FontFill.Color := claBlack;
      Font.Style := [TFontStyle.fsItalic];
      if IsImportant then
        Font.Style := [TFontStyle.fsBold]
      else
        Font.Style := [];
      if Assigned(Font.OnChanged) then
        Font.OnChanged(Font);
      Repaint;
    end;
    
    constructor TFinancialCell.Create(AOwner: TComponent);
    begin
      inherited;
      TextAlign := TTextAlign.taTrailing;
    end;
    
    procedure TFinancialCell.SetData(const Value: Variant);
    var F: Single;
      O: TFMXObject;
      S: String;
    begin
      S := Value;
      FIsImportant := S[1] = '#';
      if IsImportant then
        S := Copy(Value,2,MaxInt)
      else
        S := Value;
    
      F := StrToFloat(S);
      inherited SetData(Format('%m', [F]));
      FIsNegative := F < 0;
      ApplyStyling;
    end;
    

    And finally, update the GetValue event handler:

    procedure TForm1.Grid1GetValue(Sender: TObject; const Col, Row: Integer;
      var Value: Variant);
    var Cell: TStyledControl;
    begin
      if Col = 0 then
        Value := Row
      else if Col = 1 then
      begin
        Value := FloatToStr(Data[Row]);
        if Value > 30 then
          Value := '#'+Value;
      end;
    end;
    
    0 讨论(0)
  • 2020-12-03 23:06

    Code above is fine for versions before XE4, but for XE4 and XE5 does not work. Color and style of text is not changed.

    This is a fixed code for XE4 and XE5:

    procedure TFinancialCell.ApplyStyling;
    begin
      StyledSettings := [TStyledSetting.ssFamily, TStyledSetting.ssSize];
      if IsNegative then
        FontColor := claRed
      else
        FontColor := claBlack;
      Font.Style := [TFontStyle.fsItalic];
      if IsImportant then
        Font.Style := [TFontStyle.fsBold]
      else
        Font.Style := [];
      if Assigned(Font.OnChanged) then
        Font.OnChanged(Font);
      Repaint;
    end;
    
    0 讨论(0)
提交回复
热议问题