How can I set an icon for a ListViewSubItem?

前端 未结 7 563
既然无缘
既然无缘 2020-12-05 19:36

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?<

相关标签:
7条回答
  • 2020-12-05 20:22

    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;
    }
    
    0 讨论(0)
提交回复
热议问题