如何为 ListViewSubItem 设置图标? [英] How can I set an icon for a 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?
推荐答案
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 a DataGridView
and use a DataGridViewImageColumn
. If that is not possible, then you'll need to draw the icons yourself using the custom draw support in the ListView
control. To do this set ListView.OwnerDraw = true
and handle the ListView.DrawSubItem
and ListView.DrawColumnHeader
events.
private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
// Only interested in 2nd column.
if (e.Header != this.columnHeader2)
{
e.DrawDefault = true;
return;
}
e.DrawBackground();
var imageRect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Height, e.Bounds.Height);
e.Graphics.DrawImage(SystemIcons.Information.ToBitmap(), imageRect);
}
private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
这篇关于如何为 ListViewSubItem 设置图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!