加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – Firemonkey网格控件 – 将列对齐到右侧

发布时间:2020-12-15 09:51:52 所属栏目:大数据 来源:网络整理
导读:我正在使用FireMonkey Grid控件,但在尝试右对齐列时遇到了一个持续的问题.从其他用户发布的帖子中,我设法创建了一个新的TColumn类型,将一个样式应用于此(文本为HorzAlign = taTrailing)并且在理论上 – 认为这将是解决方案.这些值由OnGetValue函数提供给Grid
我正在使用FireMonkey Grid控件,但在尝试右对齐列时遇到了一个持续的问题.从其他用户发布的帖子中,我设法创建了一个新的TColumn类型,将一个样式应用于此(文本为HorzAlign = taTrailing)并且在理论上 – 认为这将是解决方案.这些值由OnGetValue函数提供给Grid控件.

但问题是,虽然一开始它看起来不错,但是如果你滚动条形/鼠标滚轮等,新的TColumn类型列似乎没有使用下面的方法/代码正确刷新.它可能是网格的错误/特征(或者我正在做的方式).我试过.ReAlign等…;但无济于事.让网格重新排列的唯一方法是执行列调整大小 – 然后重新正确重绘?

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

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.

亲切的问候.伊恩.

解决方法

所有这些都提醒我,我还没有写关于此的博文.

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

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

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

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

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

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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读