【发布时间】:2012-03-04 20:16:58
【问题描述】:
我正在寻找推荐的解决方案来设置由 OnGetValue 调用绘制的 TGrid 单元格的样式(调用它来绘制视图中的单元格)。作为背景,Mike 的出色回应展示了如何在创建单元格时简单地应用 tAlign 属性;但我的下一个挑战是为单元格内容着色。
目标是更改我即将作为单元格“值”返回的值的单元格属性(字体、样式、颜色等)。在下面的例子中;它将对正在返回的 OnGetValue“值”应用样式。很可能我们必须通过 FM 样式表来做到这一点;或者我们可以直接获得 TText 属性吗?理想情况下,这两种情况都很好 - 但在这个阶段我会采取任何一种解决方案......(;->
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;
published
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
function TStringColNum.CreateCellControl: TStyledControl;
begin
Result:=TTextCell.Create(Self);
TTextCell(Result).TextAlign := TTextAlign.taTrailing;
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);
begin
if Col=0 then
Value:='Row '+IntToStr(Row);
if Col=1 then
Value := 'Row '+IntToStr(Row);
// Apply style based on value ?
end;
end.
提前非常感谢, 伊恩。
【问题讨论】:
-
你能定义“基于一个值”吗?你的意思是说,如果值为负,那么字体将是红色等?
-
嗨,迈克 - 是的;发现。我有两种情况,但都是相同的原理。一种情况是负值显示为红色,另一种情况是“加粗”列表中的项目(我选择的项目 - 由于离网持有的详细信息;重要客户等......)。提前致谢。伊恩。
标签: delphi grid delphi-xe2 firemonkey