简体   繁体   English

如何使列大小适合窗口大小?

[英]How can I fit column size to window size?

How can I fit the column range of my listview to the current window size. 如何使列表视图的列范围适合当前窗口大小。 So that it will stretch in maximized window mode ? 这样它将在最大化窗口模式下拉伸?

Regardless of how you ask for it, the ListView supports only two ways of automatic column resizing: either by the length of the column content, or by the length of the column header content. 无论您如何要求,ListView仅支持两种自动调整列大小的方法:按列内容的长度或按列标题内容的长度。

Since that's apparently not what you want, you will need to write the code to do this yourself. 由于这显然不是您想要的,因此您将需要编写代码自己执行此操作。 To do so, attach a handler method to your form's ResizeEnd event. 这样做,将处理程序方法附加到窗体的ResizeEnd事件。 That event gets raised whenever your form has been resized, either by the user or programmatically through code. 每当您调整表单的大小(由用户或通过代码以编程方式)时,都会引发该事件。 Presumably, you've already used the Anchor and/or Dock properties on your ListView control to ensure that it resizes with its parent form, so this should cover all cases. 大概,您已经在ListView控件上使用了Anchor和/或Dock属性,以确保使用其父窗体调整其大小,因此这应涵盖所有情况。

Inside of that event handler method, you will calculate the new sizes of each column and adjust the ListView control accordingly. 在该事件处理程序方法内部,您将计算每列的新大小并相应地调整ListView控件。 This is the only hard part, figuring out which algorithm you want to use for column resizing. 这是唯一困难的部分,要弄清楚要用于列大小调整的算法。

If you have a ListView like this: 如果您有这样的ListView:

| Order # | Customer Name | Phone Number | Status |
|---------|---------------|--------------|--------|
|         |               |              |        |

You might decide that you want the "Order #" and "Status" columns to be both the same width and the narrowest (because they have the least amount of information to display). 您可能决定要使“订单号”和“状态”列的宽度相同且最窄(因为它们显示的信息量最少)。 The "Customer Name" needs to be the longest (because it has the most amount of information to display), and the "Phone Number" can be somewhere in the middle. “客户名称”必须是最长的(因为它显示的信息最多),“电话号码”可以在中间。

So then all you have to do is size each column proportionally to the total available width of the ListView control, which you can retrieve by querying its ClientSize property; 因此,您要做的就是与ListView控件的总可用宽度成比例地调整每一列的大小,您可以通过查询其ClientSize属性来检索该宽度。 eg, 例如,

float totalWidth = myListView.ClientSize.Width;

You'll find a really elegant and reusable demonstration of this method here . 你会发现这种方法的一个非常优雅和可重复使用的示范这里

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

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