繁体   English   中英

如何更改 cxGrid 表视图的标题对齐方式?

[英]how can I change the header alignment of my cxGrid table view?

如何更改 cxGrid tableView 的标题对齐方式? 我试试

MyGridColumn.HeaderAlignmentVert := TcxAlignmentVert.vaCenter;

但这不起作用:(

下面的代码显示了如何创建一个 cxGrid,添加一个 TcxGridDBTableView,并设置 TableView 的标题的标题对齐,所有这些都完全在代码中。 我已经在代码中完成了这一切,因为 cxGrid 及其内容的对象检查器中有太多属性,很难一眼看出(在 OI 或 DFM 中)相关属性是什么。

type
  TForm1 = class(TForm)
    CDS1: TClientDataSet;
    DS1: TDataSource;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    procedure FormCreate(Sender: TObject);
  [...]
  public
    cxGrid : TcxGrid;
    cxLevel : TcxGridLevel;
    cxView : TcxGridDBTableView;
  end;
[...]
procedure TForm1.FormCreate(Sender: TObject);
var
  Field : TField;
begin
  // First, set up TClientDataSet

  Field := TIntegerField.Create(Self);
  Field.FieldKind := fkData;
  Field.FieldName := 'ID';
  Field.DataSet := CDS1;

  Field := TStringField.Create(Self);
  Field.FieldKind := fkData;
  Field.FieldName := 'Name';
  Field.Size := 40;
  Field.DataSet := CDS1;

  Field := TIntegerField.Create(Self);
  Field.FieldKind := fkData;
  Field.FieldName := 'Value';
  Field.DataSet := CDS1;

  CDS1.CreateDataSet;

  CDS1.IndexFieldNames := 'ID';

  CDS1.InsertRecord([1, 'One', 1]);
  CDS1.InsertRecord([1, 'Two', 2]);
  CDS1.InsertRecord([1, 'Three', 3]);

  CDS1.First;

  //  Next, create cxGrid, and add cxGridDBTableView
  cxGrid := TcxGrid.Create(Self);
  cxGrid.Parent := Self;
  cxGrid.Width := 400;

  cxLevel := cxGrid.Levels.Add;
  cxLevel.Name := 'Firstlevel';

  cxView := cxGrid.CreateView(TcxGridDBTableView) as TcxGridDBTableView;
  cxView.Name := 'ATableView';
  cxView.OptionsView.HeaderHeight := 100; // so we can easily see different vert alignments

  cxView.DataController.Options := cxView.DataController.Options + [dcoImmediatePost];
  cxView.DataController.KeyFieldNames := 'ID';

  cxLevel.GridView := cxView;

  cxView.DataController.DataSource := DS1;
  cxView.DataController.CreateAllItems;

  //  by this point the columns and their headers will have been created

  cxView.Columns[0].HeaderAlignmentVert := cxclasses.vaTop;
  cxView.Columns[1].HeaderAlignmentVert := cxclasses.vaCenter;
  cxView.Columns[2].HeaderAlignmentVert := cxclasses.vaBottom;
end;

对我来说,标题有正确的对齐方式,即

ID
    Name
           Value

Fwiw,在创建上面的示例之前,我尝试在现有项目上设置标题的垂直对齐方式,但也无法使其工作,我的意思是,标题文本都顽固地停留在垂直中心。 所以我想我现有项目的网格中必须有一些其他设置覆盖了对齐行为。

更新我注意到,如果我注释掉设置 HeaderHeight 的行,垂直对齐设置似乎没有效果。 我认为原因很简单,因为对齐方式不同导致的文本垂直位置差异太小而无法注意到-只有在我将HeaderHeight增加到23或更多之后,才存在明显差异。

好的,我找到了问题,因为我设置了

OptionsView.HeaderEndEllipsis = True

当你这样做时

MyGridColumn.HeaderAlignmentVert := TcxAlignmentVert.vaCenter;

不管用 :(

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM