【问题标题】:how can I change the header alignment of my cxGrid table view?如何更改我的 cxGrid 表格视图的标题对齐方式?
【发布时间】:2020-02-19 22:18:32
【问题描述】:

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

MyGridColumn.HeaderAlignmentVert := TcxAlignmentVert.vaCenter;

但这不起作用:(

【问题讨论】:

  • “不起作用”太含糊了 - 你期待什么?
  • @Brian 我的意思是在完成 MyGridColumn.HeaderAlignmentVert := TcxAlignmentVert.vaCenter;对齐仍然在顶部:(
  • 在图像中显示 - 在这里可以正常工作。将标题高度设置为 100,将其中一个字段列的 HeaderAlignmentVert 设置为 Center,并且列标题在标题中垂直居中。
  • 为了将来参考,this not work 是一个绝对无用的问题描述,除非你准确地解释你期望发生的事情并清楚地解释它做了什么。 IOW,请解释具体如何它不起作用,所以我们有一些信息可以继续。如果您不明白原因,请致电您的汽车维修店并说我的车坏了。 x这是福特,它是蓝色的。它有什么问题?没有其他细节,看看它是如何工作的。
  • 如果您提供minimal reproducible example 来显示您在何处使用该单行代码,这也会有所帮助,因为该代码的位置是相关的。我想,您在这里待的时间已经够长了,应该知道这个网站是如何运作的。

标签: delphi devexpress tcxgrid devexpress-gridcontrol


【解决方案1】:

下面的代码展示了如何创建一个 cxGrid,添加一个 TcxGridDBTableView,并设置 TableView 的标题的标题对齐方式,全部在代码中。我已经在代码中完成了这一切,因为 cxGrid 及其内容的 Object Inspector 中有太多属性,以至于很难一目了然(在 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 或更大之后,才会有明显的差异。

【讨论】:

  • 我不知道为什么有人给你的答案 -1 并不重要。是的,我的项目中的某些东西正在覆盖对齐:(也许是样式或类似的东西?
  • 谢谢,我也不知道为什么它也被否决了。无论如何,如果你你我的代码完全一样。您看到 3 个标题文本的垂直位置有什么不同吗?然后,注释掉将 HeadeHeight 设置为 100 的行:你能看到之后垂直位置的差异吗?
【解决方案2】:

好的,我发现了问题 这是因为我设置了

OptionsView.HeaderEndEllipsis = True

当你这样做时,

MyGridColumn.HeaderAlignmentVert := TcxAlignmentVert.vaCenter;

不工作:(

【讨论】:

  • 我在答案中的示例和我提到的现有项目中得到了这种行为。
  • Developer Express 非常适合处理此类问题的功能请求/错误报告。与当前最新版本 19.2.4 一起复制。如果它尊重对齐直到它被填满然后放在省略号中会更好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
  • 2011-06-25
  • 1970-01-01
  • 2017-03-19
  • 1970-01-01
  • 2013-08-23
  • 2020-10-27
相关资源
最近更新 更多