[英]How to resize height of ListView to fit content
嘿,我一直在网上寻找一个解决方案,让我可以根据其中的最后一个条目调整列表视图的大小。
到目前为止,我只知道如何使用Telerik和Syncfusion等其他控件来做到这一点......除了这些公司之外,我只找到了根据里面的内容来调整行或列的方法 - 而不是整体高度列表视图本身。 其他示例也接近我想要做的,但适用于WPF 。
我的winform现在看起来像这样:
但我想要它做的是:
在此之前有没有人这样做可以显示他们使用的代码? 或者是否有更好的控制可以满足我的需要,可以满足我的需要? 谢谢!
向 Control 发送一个LVM_APPROXIMATEVIEWRECT
消息,您可以得到一个近似的度量,包括 Header 的高度和所有 Item 的大小。
将控件的ClientSize
设置为此度量,应该允许调整 ListView 的大小,以显示所有项目的完整范围。
您可以指定要包含的多个项目 ( wParam
) 和首选大小 ( lParam
),或者将lParam
和wParam
都设置为-1
:在这种情况下,将包含所有项目并自动检测大小。
请注意,高度可能包括水平滚动条高度:在底部可能会看到一个边距。
如果不希望这样做,请从整体高度中删除SystemInformation.HorizontalScrollBarHeight
。
或者,如果 ListView 中的项目数建议,则执行相同的操作以将 Control 的范围限制为特定度量。
► 如评论中所述,如果 ListView 停靠在 Form 上,设置其 ClientSize 没有任何可见效果。
在这种情况下,您还需要调整表单的大小,添加 ListView 的旧大小与其新计算大小之间的差异。
假设 ListView 被命名为listView1
:
Size oldSize = listView1.ClientSize;
int hScrollBarHeight = SystemInformation.HorizontalScrollBarHeight
// Both wParam and lParam set to -1: include all Items and full size
int approxSize = NativeMethods.SendMessage(
listView1.Handle, NativeMethods.LVM_APPROXIMATEVIEWRECT, -1, (-1 << 16 | -1));
int approxHeight = approxSize >> 16;
int approxWidth = approxSize & 0xFFFF;
Size newSize = new Size(approxWidth, approxHeight - hScrollBarHeight);
// If needed, resize the Form (here, grow and shrink) to adapt to the new size
// Checking the Dock property state is a possible example, apply whatever logic fits
if (listView1.Dock != DockStyle.None) {
this.Height += newSize.Height - oldSize.Height;
this.Width += newSize.Width - oldSize.Width;
}
listView1.ClientSize = newSize;
internal class NativeMethods
{
internal const int LVM_FIRST = 0x1000;
internal const int LVM_APPROXIMATEVIEWRECT = LVM_FIRST + 0x40;
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.