Delphi Firemonkey栅格控件-将柱向右对齐
我正在使用FireMonkey网格控件,但在尝试右对齐列时遇到了一个持续的问题。从其他用户的帖子中,我成功地创建了一个新的TColumn类型,对其应用了一种样式(文本为HorzAlign=tatraily),理论上,我认为这将是一个解决方案。这些值由OnGetValue函数提供给网格控件 但是,问题是,尽管一开始看起来不错,但如果滚动条/鼠标滚轮等,新的TColumn类型列似乎无法使用下面的方法/代码正确刷新。这可能是网格的一个bug/特性(或者我正在做的方式)。我已经试过了。重新校准等。。。;但是没有用。使网格重新对齐的唯一方法是调整列大小(例如),然后正确地重新绘制 下面的代码显示它是一个简单的TGrid,有2个col、1个标准StringColumn和1个my new StringColNum(应用了wuth right对齐)。-感谢任何帮助,因为这是任何网格工作的基本要求Delphi Firemonkey栅格控件-将柱向右对齐,delphi,grid,delphi-xe2,firemonkey,Delphi,Grid,Delphi Xe2,Firemonkey,我正在使用FireMonkey网格控件,但在尝试右对齐列时遇到了一个持续的问题。从其他用户的帖子中,我成功地创建了一个新的TColumn类型,对其应用了一种样式(文本为HorzAlign=tatraily),理论上,我认为这将是一个解决方案。这些值由OnGetValue函数提供给网格控件 但是,问题是,尽管一开始看起来不错,但如果滚动条/鼠标滚轮等,新的TColumn类型列似乎无法使用下面的方法/代码正确刷新。这可能是网格的一个bug/特性(或者我正在做的方式)。我已经试过了。重新校准等。。。;
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方法中创建单元格。实际上,您正在创建列的一个实例,这是您的主要问题 您不需要列的Create方法(它什么也不做),所以删除它(除非您需要它来做其他事情)并修改您的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;
列的降序在livebindings中不起作用,因为bindmanager创建列,所以您必须处理降序。在我看来既不优雅也不实用 只需在网格绘制事件中对齐单元格即可
I := Col;
for J := 0 to Grid1.RowCount - 1 do
begin
T := TTextCell(Grid1.Columns[I].Children[J]);
T.TextAlign := TTextAlign.taTrailing;
end;
如果在自定义正在创建的列类的机会较少时使用LiveBinding,但可以为设置单个单元格控件的某些属性的列创建帮助程序。不太优雅但简单实用:
unit GridColumnHelper;
interface
uses
Fmx.Types, Fmx.Controls, Fmx.Grid, Fmx.Edit;
type
TGridColumnHelper = class helper for TColumn
public
procedure SetEditMaxLength(aValue: Integer);
procedure SetEditTextAlign(aValue: TTextAlign);
end;
implementation
{ TGridColumnHelper }
procedure TGridColumnHelper.SetEditMaxLength(aValue: Integer);
var
lControl: TStyledControl;
begin
for lControl in FCellControls do
begin
if lControl is TEdit then
(lControl as TEdit).MaxLength := aValue;
end;
end;
procedure TGridColumnHelper.SetEditTextAlign(aValue: TTextAlign);
var
lControl: TStyledControl;
begin
for lControl in FCellControls do
begin
if lControl is TEdit then
(lControl as TEdit).TextAlign := aValue;
end;
end;
end.
绑定填充网格后,可以调用帮助程序:
MyGrid.Columns[0].SetEditTextAlign(TTextAlign.taTrailing);
MyGrid.Columns[1].SetEditMaxLength(15);
我认为这是英巴卡德罗的懒惰 在FMX.Grid.pas中添加/修改3行可以解决此问题 我建议不要修改原始FMX.Grid pas,而是将原始FMX.Grid pas复制到项目目录中,包括在项目中(添加到项目)并添加/修改以下行
TColumn = class(TStyledControl)
private const
HorzTextMargin = 2;
VertTextMargin = 1;
private
FReadOnly: Boolean;
FHorizontalAlign:TTextAlign;//Add this Line *********
FEditMode: Integer;
FApplyImmediately: boolean;
...
...
procedure UpdateCell(ARow: Integer);
published
property HorizontalAlign: TTextAlign read FHorizontalAlign write FHorizontalAlign;//add this line *******
property Align;
property ClipChildren default False;
过程TColumn.DefaultDrawCell(const Canvas:TCanvas;const Bounds:TRectF;const Row:Integer;
常量值:TValue;常量状态:tGrawState);
变量
R:TRectF;
布局:TTextLayout;
LocalRow:整数;
开始
如果FDrawable为零,则
DrawCell(画布、边界、行、值、状态)
其他的
...
...
布局不透明度:=绝对不透明度;
(*在这一行备注*****************
Layout.HorizontalAlign:=Grid.TextSettingsControl.ResultingTextSettings.HorzAlign;
*)
Layout.HorizontalAlign:=HorizontalAlign//添加这一行*****
最后,您可以在项目中设置新属性。e、 g:
MyColumn.HorizontalAlign:=TTextAlign.taCenter suat dmk的解决方案运行良好如果要使用数据库链接,您必须重新编译Fmx.Bind.DBLinks.pas和Fmx.Bind.Editors.pas
之后,您只需加入OnPaint事件:
SGrid1.ColumnByIndex(1).HorizontalAlign := TTextAlign.Leading;
另一个解决方案:
Grid1.ApplyStyleLookup();
MyCol1.DefaultTextSettings.HorzAlign:=TTextAlign.taCenter;
谢谢迈克——我一直在寻找的“完美答案”;一个真正的超级明星。。!一个解释,发布的代码做了什么,以及它如何解决问题,很少不能改善答案。
Grid1.ApplyStyleLookup();
MyCol1.DefaultTextSettings.HorzAlign:=TTextAlign.taCenter;