Firemonkey网格控制 - 将列对齐到右侧 [英] Firemonkey Grid Control - Aligning a column to the right

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

问题描述

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



然而,问题是,虽然起初看起来不错,如果您滚动条/鼠标滚轮等新的TColumn类型列似乎不会使用下面的方法/代码正确刷新。它可能是网格的错误/功能(或者我正在做的)。我试过了。但无济于事将网格重新排列的唯一方法是进行列重新调整大小,然后正确重绘?



下面的代码显示它是一个简单的TGrid, 2个cols,1个标准StringColumn和1个我的新StringColNum(应用了wuth权限对齐)。 - 任何帮助赞赏,因为这是任何电网工作的基本要求。

  unit Unit1; 

接口

使用
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;
程序Grid1GetValue(发件人:TObject; const Col,Row:Integer;
var Value:Variant);
procedure Button1Click(Sender:TObject);
private
{私人声明}
public
{公开声明}
end;

TStringColNum = class(TStringColumn)
private
函数CreateCellControl:TStyledControl;覆盖
public
构造函数Create(AOwner:TComponent);覆盖
发布
end;

var
Form1:TForm1;

实现

{$ R * .fmx}

构造函数TStringColNum.Create(AOwner:TComponent);
开始
继承;
结束

函数TStringColNum.CreateCellControl:TStyledControl;
var
t:TEdit;
begin
结果:= TStringColNum.Create(Self);
Result.StyleLookup:='textrightalign';
结束

procedure TForm1.Button1Click(Sender:TObject);
begin
Grid1.AddObject(TStringColumn.Create(Self));
Grid1.AddObject(TStringColNum.Create(Self)); //右对齐列?

Grid1.RowCount:= 5000;
Grid1.ShowScrollBars:= True;
结束

程序TForm1.Grid1GetValue(发件人:TObject; const Col,Row:Integer;
var Value:Variant);
var
单元格:TStyledControl;
t:TText;
begin
如果Col = 0,那么
值:='Row'+ IntToStr(Row);;

如果Col = 1然后
begin
单元格:= Grid1.Columns [Col] .CellControlByRow(Row);
如果分配(单元格)然后
开始
t:=(Cell.FindStyleResource('text')为TText);
如果分配(t)然后
t.Text:='Row'+ IntToStr(Row);
结束
结束
结束

结束。

亲切的问候。 Ian。

解决方案

所有这些都提醒我,我还没有写我的博客文章。 b
$ b

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



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



您不需要您的列的Create方法(它什么都不做),所以删除它(除非你需要其他东西)并修改你的CreateCellControl。

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

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

  procedure TForm1.Grid1GetValue(Sender:TObject; const Col,Row:Integer; 
var Value:Variant);
begin
如果Col = 0,那么
值:='Row'+ IntToStr(Row);

如果Col = 1然后
值:='Row'+ IntToStr(Row);
结束


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.

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?

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.

Kind regards. Ian.

解决方案

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

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).

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.

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;

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网格控制 - 将列对齐到右侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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