【问题标题】:VirtualStringTree columns should adapt size when one column is hidden当隐藏一列时,VirtualStringTree 列应调整大小
【发布时间】:2013-11-27 12:10:22
【问题描述】:

我有一个包含 3 列的 VST,它们平均占用可用空间。

  • 我在Header.Options中设置了hoAutoSpring,并且
  • 所有列都有 Column[x].OptioncoAutoSpring 集。

现在我希望能够隐藏最后一列并保持其他列均匀占用可用空间(有点像带有alClient 的控件)。

当我只将列设置为不可见时(见下文),该列占用的空间就根本未被使用。

VST.Header.Columns[2].Options:=VST.Header.Columns[2].Options - [coVisible];

当我将 Header.Options.hoAutoResize 设置为 True 并将 Header.AutoSizeIndex 设置为 1 时,第二列将占用所有的新空间。

有没有办法告诉列填满可用空间并均匀调整大小?

截图:

【问题讨论】:

  • 单独为图片+1。第一个问题很好。欢迎来到 SO!
  • 原来AutoFitColumnssmaAllColumns 并没有表现出我预期的行为。我删除了我的答案。
  • @jpfollenius:谢谢!我很惊讶我在这里找到高质量答案的速度如此之快。我想我会在这里呆一段时间:)

标签: delphi virtualtreeview tvirtualstringtree


【解决方案1】:

感谢大家非常快速和高质量的回复!

由于似乎没有内置的方法来解决我的问题,我已经编写了以下方式(以防万一有人遇到类似问题):

// Show/hide a column and spread the space on all other visible columns
//   so that the proportions remain the same (as if AutoSpring was used)
procedure ChangeColumnVisibility(Tree: TVirtualStringTree; Column: TColumnIndex;
  NewVisible: boolean);
var Col : TVirtualTreeColumn;
begin
     Col:=Tree.Header.Columns[Column];
     if not (NewVisible xor (coVisible in Col.Options)) then
        Exit;

     if not NewVisible then
     begin
          Col.Options:=Col.Options - [coVisible];
          Tree.Header.ResizeColumns(Col.Width, 0, Tree.Header.Columns.Count-1);
     end
     else
     begin
          Tree.Header.ResizeColumns(-Col.Width, 0, Tree.Header.Columns.Count-1);
          Col.Options:=Col.Options + [coVisible];
     end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
     ChangeColumnVisibility(VST, 2, not (coVisible in VST.Header.Columns[2].Options));
end;

【讨论】:

  • 您是否尝试过以这种方式显示您隐藏的同一列?通过手动设置“填充”列宽,如果再次显示它,您可以为该自动调整列保留一个小空间。
  • 你是对的。 Header.Options.hoAutoResize 必须为 false 才能正常工作。 (我已经设置了 Header.options.hoAutoSpring 并且我已经为我的应用程序中的所有列设置了 coAutoSpring)
  • 刚刚偶然发现 Tree.Header.ResizeColumns 这使代码更加清晰 - 在我上面的帖子中编辑了代码。
猜你喜欢
  • 2011-05-14
  • 2020-08-23
  • 2018-01-17
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
相关资源
最近更新 更多