简体   繁体   English

如何获取 ListView 项的高度?

[英]How can I get the height of a ListView item?

I have ListView and I need to determine item height.我有 ListView,我需要确定项目高度。

You can use the GetItemRect() method:您可以使用GetItemRect()方法:

int itemHeight = yourListView.GetItemRect(itemIndex).Height;

I had the same question however there is one issue - until the listview is drawn the values aren't set.我有同样的问题,但是有一个问题 - 在绘制列表视图之前,没有设置值。 And you may want to be able to set the sizes before you add any items (if for example I want to dry a listview that can display 5 entries but will start off empty).而且您可能希望能够在添加任何项目之前设置大小(例如,如果我想干燥一个可以显示 5 个条目但开始为空的列表视图)。

Therefore my workaround was to run the following code, which forces the control to be rendered but without displaying it, in the application's initialisation section and save the values as global variables for later use.因此,我的解决方法是在应用程序的初始化部分运行以下代码,强制呈现控件但不显示它,并将值保存为全局变量以供以后使用。 And, to get around listviews with different font sizes, to only store the difference between the height and the font height:而且,要绕过具有不同字体大小的列表视图,只存储高度和字体高度之间的差异:

Dim lvwTemp As New ListView
lvwTemp.View = View.Details
lvwTemp.Columns.Add("test")
lvwTemp.Items.Add("test")
Dim zTempBitmap As New Bitmap(100, 100)
lvwTemp.DrawToBitmap(zTempBitmap, New Rectangle(0, 0, 100, 100))
zTempBitmap.Dispose()
gintListviewHeaderHeightMinusFontSize = lvwTemp.Items(0).GetBounds(ItemBoundsPortion.Entire).Top - lvwTemp.Font.Height
gintListviewItemHeightMinusFontSize = lvwTemp.Items(0).GetBounds(ItemBoundsPortion.Entire).Height - lvwTemp.Font.Height

I am not 100% sure but this might help:我不是 100% 确定,但这可能会有所帮助:

int itemHeight = listView.Items[itemIndex].GetBounds(ItemBoundsPortion.Entire).Height;

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

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