如何为 ListViewSubItem 设置图标? [英] How can I set an icon for a ListViewSubItem?

查看:31
本文介绍了如何为 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.DrawSubItemListView.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆