gpt4 book ai didi

delphi - Firemonkey 网格控件 - 根据值设置单元格样式(通过 OnGetValue 函数调用)

转载 作者:行者123 更新时间:2023-12-03 14:53:30 30 4
gpt4 key购买 nike

我正在寻找推荐的解决方案来设置由 OnGetValue 调用(调用以绘制 View 中的单元格)绘制的 TGrid 单元格的样式。对于背景,Mike 的出色回应展示了如何在创建单元格时简单地应用 tAlign 属性;但我的下一个挑战是给单元格内容着色。

Previous posting/answer

目标是更改我要作为单元格“值”返回的值的单元格属性(字体、样式、颜色等)。在下面的例子中;它将对返回的 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.

提前非常感谢,伊恩。

最佳答案

首先,道歉。在我对上一个问题的回答中,CreateCellControl 应该调用继承来创建单元格。我修改了我的答案。

对于这个问题,我已经在 FireMonkey Cells 上上传了我的博客文章 - http://monkeystyler.com/blog/entry/firemonkey-grid-basics-custom-cells-and-columns - 它涵盖了之前答案中的内容,还涵盖了创建自定义单元格控件。在继续之前,您需要阅读该内容。我会等。

...

现在回来?很好。

遵循博客文章中的示例。

除此之外,我已将 TFinancialCell 更新为直接从 TTextCell(当然是 TEdit)继承,这更有意义并且样式也更简单。

因此,更新 TFinancialCell:

type TFinancialCell = class(TTextCell)
private
FIsNegative: Boolean;
FIsImportant: Boolean;
protected
procedure SetData(const Value: Variant); override;
procedure ApplyStyle;override;
procedure ApplyStyling;
public
constructor Create(AOwner: TComponent); override;
published
property IsNegative: Boolean read FIsNegative;
property IsImportant: Boolean read FIsImportant;
end;

上述代码:

procedure TFinancialCell.ApplyStyle;
var T: TFMXObject;
begin
inherited;
ApplyStyling;
end;

procedure TFinancialCell.ApplyStyling;
begin
if IsNegative then
FontFill.Color := claRed
else
FontFill.Color := claBlack;
Font.Style := [TFontStyle.fsItalic];
if IsImportant then
Font.Style := [TFontStyle.fsBold]
else
Font.Style := [];
if Assigned(Font.OnChanged) then
Font.OnChanged(Font);
Repaint;
end;

constructor TFinancialCell.Create(AOwner: TComponent);
begin
inherited;
TextAlign := TTextAlign.taTrailing;
end;

procedure TFinancialCell.SetData(const Value: Variant);
var F: Single;
O: TFMXObject;
S: String;
begin
S := Value;
FIsImportant := S[1] = '#';
if IsImportant then
S := Copy(Value,2,MaxInt)
else
S := Value;

F := StrToFloat(S);
inherited SetData(Format('%m', [F]));
FIsNegative := F < 0;
ApplyStyling;
end;

最后,更新 GetValue 事件处理程序:

procedure TForm1.Grid1GetValue(Sender: TObject; const Col, Row: Integer;
var Value: Variant);
var Cell: TStyledControl;
begin
if Col = 0 then
Value := Row
else if Col = 1 then
begin
Value := FloatToStr(Data[Row]);
if Value > 30 then
Value := '#'+Value;
end;
end;

关于delphi - Firemonkey 网格控件 - 根据值设置单元格样式(通过 OnGetValue 函数调用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9260355/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com