FireMonkey XE5 - Livebinding - TGrid - Cell text aligment

放肆的年华 提交于 2019-12-06 08:00:44

Use the OnDrawColumnCell Event.

For columns containing text cells, the text layout information for each individual Column is assigned from the TextSettings property of the grid. However, the assignment is performed prior to the event firing.

The best and simplest way is to just directly access the layout for a specific column via a class helper before any drawing takes place.

Set the DefaultDrawing property of the grid to False and paste the following code:

interface
  type
    TColumnHelper = class helper for FMX.Grid.TColumn
        function getTextLayout: TTextLayout;
    end;

implementation

{ TColumnHelper }    
function TColumnHelper.getTextLayout: TTextLayout;
begin
  Result := Self.FDrawLayout;
end;

{ OnDrawColumnCell }
procedure GridDrawColumnCell(Sender: TObject; const Canvas: TCanvas;
  const Column: TColumn; const Bounds: TRectF; const Row: Integer;
  const Value: TValue; const State: TGridDrawStates);
begin

  { change text layout info prior to default drawing }
  if Column.Header = 'Numerical Column' then
    Column.getTextLayout.HorizontalAlign := TTextAlign.Trailing
  else
    Column.getTextLayout.HorizontalAlign := TGrid(Sender).TextSettings.HorzAlign;

  { perform default drawing }
  TGrid(Sender).DefaultDrawColumnCell(Canvas, Column, Bounds, Row, Value, State);
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!