如何为 ListViewSubItem 设置图标?
在 ListView 中,每个项目都可以有图标。
在详细信息模式下查看时,该图标显示在最左侧的列中。
我可以在其他列中显示图标吗?
In a ListView you can have icons on each item.
When viewing in Details-mode, the icon is shown in the left-most column.
Can I show an icon in some other column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
ListView
控件本身不支持子项中的图像。最简单的方法是切换到 DataGridView 并使用 DataGridViewImageColumn。如果这是不可能的,那么您需要使用ListView
控件中的自定义绘制支持自行绘制图标。为此,请设置ListView.OwnerDraw = true
并处理ListView.DrawSubItem
和ListView.DrawColumnHeader
事件。The
ListView
control does not support images in sub-items natively. The easiest thing to do is switch to aDataGridView
and use aDataGridViewImageColumn
. If that is not possible, then you'll need to draw the icons yourself using the custom draw support in theListView
control. To do this setListView.OwnerDraw = true
and handle theListView.DrawSubItem
andListView.DrawColumnHeader
events.使用 P/Invoke 并向listview(您应该在创建控件时设置LVS_EX_SUBITEMIMAGES样式或通过LVM_SETEXTENDEDLISTVIEWSTYLE),指定子项索引和相应的图像索引。您需要对插入的每个列表项执行此操作。
Use P/Invoke and send LVM_SETITEM message to the listview (you should set LVS_EX_SUBITEMIMAGES style on control creation or via LVM_SETEXTENDEDLISTVIEWSTYLE), specify the subitem index and the corresponding image index. You will need to do it for every list item you insert.
ObjectListView 是 .NET Winforms ListView 的开源包装器。它使用 @ligget78 提到的 p/invoke 策略支持子项上的图像。它还解决了 ListView 的许多其他常见问题。
它允许您以最小的努力制作非常漂亮的 ListView:
(来源:sourceforge.net)
ObjectListView is an open source wrapper around a .NET Winforms ListView. It supports images on subitems using the p/invoke strategy that that @ligget78 mentioned. It also solves many other common problems with a ListView.
It allows you to make very nice looking ListViews with a minimum effort:
(source: sourceforge.net)
继承ListView,绘制自己的图标。
Inherit from ListView and draw your own icons.
图标显示在“第一”列中,这也是键盘前缀搜索的基础。一种可能的解决方案是通过将第一列的 DisplayIndex 设置为其他值来重新排序列。
当然,只有当您只需要一列中的图标时,这才有效。
The icon is shown in the "first" column, and this is also the basis for the keyboard prefix search. One possible solution could be to reorder the columns by setting the DisplayIndex of the first column to something else.
This of course only works if you need an icon in only one column.
.NET 对此没有支持。
看看这个项目。
There's no .NET support for this.
Have a look at this project.
抓住这个:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/d25b4ffa-2ea4-43cd-a3ae-8dd0387197ae/
除了接受的答案之外,您还应该将 DrawItem 事件处理为好吧,否则行不通。
Take a loot at this:
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/d25b4ffa-2ea4-43cd-a3ae-8dd0387197ae/
In addition to the accepted answer, you should handle the DrawItem event as well, or it will not work.