简体   繁体   English

如何为 firemonkey TGrid/TStringGrid 中的特定列设置文本对齐方式?

[英]How to set text alignment for a specific column in firemonkey TGrid/TStringGrid?

In firemonkey (RAD Studio 10.3), I am working with a TStringGrid connected to a database and I want to change the text alignment of a specific column.在 firemonkey (RAD Studio 10.3) 中,我正在使用连接到数据库的 TStringGrid,我想更改特定列的文本对齐方式。 How can I do that?我怎样才能做到这一点? Changing HorzAlign in TextSettings property, changes the alignment of all columns.更改 TextSettings 属性中的 HorzAlign,会更改所有列的对齐方式。

I tried the suggested solution in this page and did not work!我尝试了此页面中建议的解决方案,但没有奏效! In newer versions of Firemonkey the below solution code results in an error.在较新版本的 Firemonkey 中,以下解决方案代码会导致错误。

type TSpecificColumn = class(TColumn)
protected
  function CreateCellControl: TStyledControl;override;
end;

There is no CreateCellControl function in TColumn Class anymore to be Overrided! TColumn 类中不再有 CreateCellControl 函数被覆盖! This is the error I got:这是我得到的错误:

Method CreateCellControl not found in base class.在基类中找不到方法 CreateCellControl。

In the OnDrawColumnCell and/or OnDrawColumnHeader events you can use a TTextLayout for the purpose.OnDrawColumnCell和/或OnDrawColumnHeader事件中,您可以为此使用TTextLayout As in the following example showing drawing the cells with three different alignments.如以下示例所示,绘制具有三种不同对齐方式的单元格。 The same can be applied when drawing the headers:绘制标题时也可以应用相同的方法:

uses
  ...
  fmx.textlayout;


procedure TForm11.Grid1DrawColumnCell(Sender: TObject; const Canvas: TCanvas;
  const Column: TColumn; const Bounds: TRectF; const Row: Integer;
  const Value: TValue; const State: TGridDrawStates);
var
  tl: TTextLayout;
  rf: TRectF;    // added
begin
  tl := TTextLayoutManager.DefaultTextLayout.Create;
  try
    tl.BeginUpdate;
    try
      // added from here
      rf := Bounds;
      InflateRect(rf, -2, -2);
      if (TGridDrawState.Selected in State) or
         (TGridDrawState.Focused in State) or
         (TGridDrawState.RowSelected in State)
      then
        Canvas.Fill.Color := TAlphaColors.LightBlue
      else
        Canvas.Fill.Color := TAlphaColors.White;

      Canvas.FillRect(rf, 0, 0, [], 1);
      // added until here

      tl.TopLeft := Bounds.TopLeft;
      tl.MaxSize := PointF(Column.Width, Column.Height);
      tl.Font.Size := 15;
      tl.Text := 'Some text'; // Value
      case Column.Index of
        0: tl.HorizontalAlign := TTextAlign.Leading;
        1: tl.HorizontalAlign := TTextAlign.Center;
        2: tl.HorizontalAlign := TTextAlign.Trailing;
      end;
    finally
      tl.EndUpdate;
    end;
    tl.RenderLayout(Canvas);
  finally
    tl.Free;
  end;
end;

在此处输入图片说明

TTextLayout has many other useful options and properties, so I recommend to take a look at the documentation. TTextLayout有许多其他有用的选项和属性,因此我建议您查看文档。

Apart from the change already identified InflateRect(rf, 0, 0) , to allow for existing data to be displayed correctly:除了已经确定的更改InflateRect(rf, 0, 0) ,以允许正确显示现有数据:

Change改变

tl.Text := 'Some text'; // Value line 

to

tl.Text := [StringGrid name property].Cells[Column.Index,Row];

This worked for me.这对我有用。

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

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