我怎样才能设置一个ListViewSubItem图标? [英] How can I set an icon for a ListViewSubItem?

查看:443
本文介绍了我怎样才能设置一个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屋!

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