TStringGrid 的 FMX 自定义标头

问题描述

我正在使用此代码为我的 TStringGrid (FMX - 10.4.1) 设置列标题

procedure TForm1.StringGrid1ApplyStyleLookup(Sender: TObject);
var
  Header: THeader;
  HeaderItem: THeaderItem;
  I: Integer;
begin
  Header:= THeader((Sender as TStringGrid).FindStyleResource('header'));
  if Assigned(Header) then
    begin
      for I := 0 to pred(Header.Count) do
        begin
          HeaderItem:= Header.Items[I];
          HeaderItem.StyledSettings := HeaderItem.StyledSettings - [TStyledSetting.Size,TStyledSetting.FontColor];
          HeaderItem.Font.Size := 20;
          HeaderItem.FontColor:= TAlphaColors.Blue;
          HeaderItem.TextSettings.HorzAlign := TTextAlign.Center;
          HeaderItem.TextSettings.VertAlign := TTextAlign.Center;
      end;
      Header.Height := 28;
  end;
end;

我得到了预期的结果

enter image description here

但是,如果我用一些新数据更新列表,标题将恢复为默认样式

enter image description here

为什么现在不一样了?为什么 ApplyStyleLookup 只应用一次?

我如何确保每次都将正确的设置应用于我的标题? 谢谢

下面是示例代码

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 400
  ClientWidth = 600
  Position = DesktopCenter
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  DesignerMasterStyle = 0
  object StringGrid1: TStringGrid
    Align = Client
    CanFocus = True
    ClipChildren = True
    Margins.Left = 5.000000000000000000
    Margins.Top = 50.000000000000000000
    Margins.Right = 5.000000000000000000
    Margins.Bottom = 5.000000000000000000
    Size.Width = 590.000000000000000000
    Size.Height = 345.000000000000000000
    Size.PlatformDefault = False
    StyleLookup = 'gridstyle'
    TabOrder = 0
    RowCount = 0
    Options = [ColumnResize,ColLines,RowLines,RowSelect,Tabs,Header]
    OnApplyStyleLookup = StringGrid1ApplyStyleLookup
    Viewport.Width = 586.000000000000000000
    Viewport.Height = 320.000000000000000000
    object StringColumn1: TStringColumn
      Header = 'Test'
    end
  end
  object Button1: TButton
    Position.X = 8.000000000000000000
    Position.Y = 8.000000000000000000
    Size.Width = 177.000000000000000000
    Size.Height = 33.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 1
    Text = 'Show Form Properties'
    OnClick = Button1Click
  end
  object Text1: TText
    Anchors = [akLeft,akTop,akRight]
    Position.X = 192.000000000000000000
    Position.Y = 8.000000000000000000
    Size.Width = 401.000000000000000000
    Size.Height = 33.000000000000000000
    Size.PlatformDefault = False
    Text = 'Unkown'
    TextSettings.HorzAlign = Trailing
  end
end


unit Unit1;

interface

uses
  System.SysUtils,System.Types,System.UITypes,System.Classes,System.Variants,System.Rtti,System.TypInfo,FMX.Types,FMX.Controls,FMX.Forms,FMX.Graphics,FMX.Dialogs,FMX.Grid.Style,FMX.StdCtrls,FMX.Controls.Presentation,FMX.ScrollBox,FMX.Grid,FMX.Header,FMX.Objects;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    Text1: TText;
    StringColumn1: TStringColumn;
    procedure Button1Click(Sender: TObject);
    procedure StringGrid1ApplyStyleLookup(Sender: TObject);
  private
    { Private declarations }
    FCount: cardinal;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation


{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
var
  PropList: PPropList;
  PropCount,PropIndex: Integer;
begin
  StringGrid1.ClearColumns;

  PropCount:= GetPropList(Form1,PropList);
  StringGrid1.RowCount:= PropCount;
  StringGrid1.RowHeight:= 20;

  StringGrid1.AddObject(TStringColumn.Create(StringGrid1));
  StringGrid1.Columns[0].Width:= (StringGrid1.Width - 24) / 2;
  StringGrid1.Columns[0].HorzAlign:= TTextAlign.Leading;
  StringGrid1.Columns[0].Header:= 'Property';

  StringGrid1.AddObject(TStringColumn.Create(StringGrid1));
  StringGrid1.Columns[1].Width:= (StringGrid1.Width - 24) / 2;
  StringGrid1.Columns[1].HorzAlign:= TTextAlign.Leading;
  StringGrid1.Columns[1].Header:= 'Value';

  for PropIndex:= 0 to pred(PropCount) do
    begin
      StringGrid1.Cells[0,PropIndex]:= PropList[PropIndex].Name;
      StringGrid1.Cells[1,PropIndex]:= GetPropValue(Form1,PropList[PropIndex].Name,true);
  end;
end;

procedure TForm1.StringGrid1ApplyStyleLookup(Sender: TObject);
var
  Header: THeader;
  HeaderItem: THeaderItem;
  I: Integer;
begin
  inc(FCount);
  Text1.Text:= Format('Executed [%.3d]',[FCount]);

  Header:= THeader((Sender as TStringGrid).FindStyleResource('header'));
  if Assigned(Header) then
    begin
      for I := 0 to pred(Header.Count) do
        begin
          HeaderItem:= Header.Items[I];
          HeaderItem.StyledSettings := HeaderItem.StyledSettings - [TStyledSetting.Size,TStyledSetting.FontColor];
          HeaderItem.Font.Size := 20;
          HeaderItem.FontColor:= TAlphaColors.Blue;
          HeaderItem.TextSettings.HorzAlign := TTextAlign.Center;
          HeaderItem.TextSettings.VertAlign := TTextAlign.Center;
      end;
      Header.Height := 28;
  end;
end;

end.

解决方法

我无法回答“为什么”的问题,除非是“设计的”。

但要解决您的问题,请致电

StringGrid1.NeedStyleLookup;

在您对网格的结构(列数/行数)进行更改之后。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...