Firemonkey Grid Control - 将列向右对齐 [英] Firemonkey Grid Control - Aligning a column to the right

查看:28
本文介绍了Firemonkey Grid Control - 将列向右对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 FireMonkey Grid 控件,但在尝试右对齐列时一直存在问题.从其他用户的帖子中,我设法创建了一个新的 TColumn 类型,对其应用样式(文本为 HorzAlign=taTrailing),理论上 - 认为这将是解决方案.这些值由 OnGetValue 函数提供给 Grid 控件.

I am using the FireMonkey Grid control but have an on-going issue in trying to right align a column. From other users postings, I have managed to create a new TColumn type, apply a style to this (text as HorzAlign=taTrailing) and in theory - thought that this would be solution. The values are provided by the OnGetValue function to the Grid control.

然而问题是,虽然起初看起来不错,但如果您滚动条/鼠标滚轮等,新的 TColumn 类型列似乎无法使用下面的方法/代码正确刷新.它可能是网格的错误/功能(或我这样做的方式).我试过 .ReAlign 等...;但无济于事.让网格重新对齐的唯一方法是调整列大小 - 然后正确重绘?

The problem is however that although at first it looks OK, if you scroll the bar/mouse wheel etc. the new TColumn type column does not appear to refresh correctly using the method/code below. It could be a bug/feature of the Grid (or the way I am doing it). I have tried .ReAlign etc...; but to no avail. The only way to get the grid back in line is do a column resize for example - which then redraws correctly?

下面的代码显示它是一个简单的 TGrid,有 2 个列,1 个标准 StringColumn 和 1 个我的新 StringColNum(应用右对齐).- 任何帮助表示赞赏,因为这是任何网格工作的基本要求.

The code below shows that it is a simple TGrid, with 2 cols, 1 the standard StringColumn and 1 my new StringColNum (wuth right alignment applied). - Any help appreciated as this one is a basic requirement of any grid work.

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.Objects, FMX.Grid,
  FMX.Layouts, FMX.Edit;

type
  TForm1 = class(TForm)
    Grid1: TGrid;
    Button1: TButton;
    StyleBook1: TStyleBook;
    procedure Grid1GetValue(Sender: TObject; const Col, Row: Integer;
      var Value: Variant);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TStringColNum = class(TStringColumn)
  private
    function CreateCellControl: TStyledControl; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

constructor TStringColNum.Create(AOwner: TComponent);
begin
  inherited;
end;

function TStringColNum.CreateCellControl: TStyledControl;
var
  t:TEdit;
begin
  Result:=TStringColNum.Create(Self);
  Result.StyleLookup := 'textrightalign';
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Grid1.AddObject(TStringColumn.Create(Self));
  Grid1.AddObject(TStringColNum.Create(Self)); // Right Aligned column?

  Grid1.RowCount:=5000;
  Grid1.ShowScrollBars:=True;
end;

procedure TForm1.Grid1GetValue(Sender: TObject; const Col, Row: Integer;
  var Value: Variant);
var
  cell: TStyledControl;
  t: TText;
begin
  if Col=0 then
    Value:='Row '+IntToStr(Row);;

  if Col=1 then
    begin
      cell := Grid1.Columns[Col].CellControlByRow(Row);
      if Assigned(cell) then
        begin
          t := (Cell.FindStyleResource('text') as TText);
          if Assigned(t) then
            t.Text:='Row '+IntToStr(Row);
        end;
    end;
end;

end.

亲切的问候.伊恩.

推荐答案

所有这些都提醒我,我还没有写过关于这个的博客文章.

All of which reminds me that I still haven't written my blog post about this.

无论如何,网格单元格可以是 TStyledControl 的任何后代(基本上是任何控件).文本单元格的默认值是 TTextCell,它只是一个 TEdit.作为 TEdit 意味着更改对齐方式非常简单:只需更改 TextAlign 属性.无需弄乱样式(除非您真的想这样做).

Anyway, a grid cell can be any descendant of TStyledControl (basically any control). The default for a text cell is TTextCell, which is simply a TEdit. Being a TEdit means changing the alignment is really easy: just change the TextAlign property. No need to mess with styles (unless you really want to).

您的列需要在 CreateCellControl 方法中创建您的单元格.您实际上是在创建列的实例,这是您的主要问题.

Your column needs to create your cells in the CreateCellControl method. You're actually creating an instance of your column which is your main problem.

您的列不需要 Create 方法(它什么都不做),所以删除它(除非您需要它来做其他事情)并修改您的 CreateCellControl.

You don't need the Create method for your column (it's doing nothing), so delete it (unless you need it for something else) and amend your CreateCellControl.

function TStringColNum.CreateCellControl: TStyledControl;
begin
  Result:=inherited;
  TTextCell(Result).TextAlign := taTrailing;
end;

最后,您的 GetValue 事件处理程序只需返回值即可:

Finally, your GetValue event handler needs do nothing more than return the value:

procedure TForm1.Grid1GetValue(Sender: TObject; const Col, Row: Integer;
  var Value: Variant);
begin
  if Col=0 then
    Value:='Row '+IntToStr(Row);

  if Col=1 then
    Value := 'Row '+IntToStr(Row);
end;

这篇关于Firemonkey Grid Control - 将列向右对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆