C# WinForms ListView 中的可变高度行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/626037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:48:35  来源:igfitidea点击:

Variable height rows in WinForms ListView

c#.netwinformslistview

提问by dbkk

Is it possible to have variable-height rows within a WinForms ListView in Details mode?

在详细信息模式下,WinForms ListView 中是否可以有可变高度的行?

There is no RowHeight or Rows[i].Height property on the control as far as I know.

据我所知,控件上没有 RowHeight 或 Rows[i].Height 属性。

Some blogs suggests implementing OwnerDraw, which I did, but I still can't find anything resembling height property from within the event handlers.

一些博客建议实现 OwnerDraw,我做到了,但我仍然无法从事件处理程序中找到任何类似于高度属性的内容。

Ideally, the row height would auto-size to show multiline text when needed.

理想情况下,行高会在需要时自动调整大小以显示多行文本。

采纳答案by lakshmanaraj

One option to think of:

一种选择:

To override the item height of all rows, create a dummy ImageList and set it to the desired height and assign it to the listview depending on the view or grasp the concept of http://www.codeproject.com/KB/list/changerowheight.aspx

要覆盖所有行的项目高度,创建一个虚拟的ImageList并将其设置为所需的高度并根据视图将其分配给listview或掌握http://www.codeproject.com/KB/list/changerowheight的概念.aspx

If you use ObjectListView, you can change the item height of all rows pretty easily. But still, there is no way to change the individual item height as long as you are using ListView.

如果使用ObjectListView,则可以非常轻松地更改所有行的项目高度。但是,只要您使用 ListView,就无法更改单个项目的高度。

回答by Marc Gravell

If you are using details mode, I wonder if DataGridViewwouldn't be more versatile. Each row has a Height, or you can use AutoSizeRowsMode to do it automatically. See MSDN"Resizing Columns and Rows in the Windows Forms DataGridView Control".

如果您使用详细信息模式,我想知道是否DataGridView会更通用。每行都有一个高度,或者您可以使用 AutoSizeRowsMo​​de 自动执行此操作。请参阅MSDN“在 Windows 窗体 DataGridView 控件中调整列和行的大小”。

回答by Bevan

The Windows ListView control itself (which is wrapped by the .NET ListView control) doesn't support variable row heights.

Windows ListView 控件本身(由 .NET ListView 控件包装)不支持可变行高。

If you stick with the ListView, you'll need to have fixed height rows.

如果您坚持使用 ListView,则需要具有固定高度的行。

回答by Chris Thompson

The ListBox control does support variable height rows, but you have do all the drawing yourself.

ListBox 控件确实支持可变高度的行,但您必须自己完成所有绘图。

Set the DrawMode to OwnerDrawVariable

将 DrawMode 设置为 OwnerDrawVariable

Then add

然后加

protected override void OnDrawItem(DrawItemEventArgs e)
{
  /* Drawing code here */
}

protected override void OnMeasureItem(MeasureItemEventArgs e)
{
  /* Measure code here */
}

I use an owner-drawn listbox in a program called Task Reporter to list each task the user entered. Each entry is differently depending on how much text is entered.

我在名为 Task Reporter 的程序中使用所有者绘制的列表框来列出用户输入的每个任务。每个条目都不同,具体取决于输入的文本量。

回答by Ken Wootton

If variable height rows are what you want, I'd consider using the DataGridView instead. It very much supports variable height rows (through the use of cell styles) and is much easier to use than trying to shoehorn the list view into doing what you want.

如果可变高度行是您想要的,我会考虑使用 DataGridView 代替。它非常支持可变高度的行(通过使用单元格样式),并且比试图将列表视图硬塞成你想做的事情更容易使用。

回答by Grammarian

The ObjectListView mentioned in the first answer does not support variable row heights. It says this clearly in its FAQ. The underlying Windows listview control simply does not support variable row height. You have to look to other, custom written controls.

第一个答案中提到的 ObjectListView 不支持可变行高。它在其常见问题解答中清楚地说明了这一点。底层的 Windows 列表视图控件根本不支持可变行高。您必须查看其他自定义编写的控件。

You may want to consider Matthew Hall's excellent XPTableand its update project, as well as Lee Paul Alexander's fantastic Outlook-style list.

你可能要考虑马修·霍尔的优秀XPTable其更新项目,以及李保罗亚历山大的神奇的Outlook风格的列表

回答by Libor

If you are OK with 3rd party components, Better ListViewsupports exactly this (each item has a CustomHeightproperty so each can have different height):

如果您对 3rd 方组件没问题,Better ListView正好支持这一点(每个项目都有一个CustomHeight属性,因此每个项目都可以有不同的高度):

enter image description here

在此处输入图片说明