I have a System.Windows.Forms.ListView. Under normal (non-virtual) operation, the ListViewItems have their images correctly shown. However, when I try to convert the ListView to virtual mode, the items no longer have their images displayed. Everything else (text, index, etc.) is displayed as intended.
The ListView is in Details mode. SmallImageList is correctly set. Each ListViewItem has its ImageKey correctly set. The ImageList has been correctly populated.
Why is the image not being displayed on a virtual ListView? What can I do to ensure that the image is displayed as intended?
Edit: Please see the following code as an example that demonstrates the problem I am having.
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new VirtualListViewExample());
}
}
class VirtualListViewExample : Form
{
public VirtualListViewExample()
{
Image image = new Bitmap(16, 16);
using (Graphics g = Graphics.FromImage(image))
{
g.FillRectangle(Brushes.Black, 0, 0, 16, 16);
}
ImageList imageList = new ImageList();
imageList.ColorDepth = ColorDepth.Depth32Bit;
imageList.ImageSize = new Size(16, 16);
imageList.Images.Add("key", image);
ListView normalList = new ListView();
normalList.Columns.Add("Column");
normalList.Dock = DockStyle.Fill;
normalList.View = View.Details;
normalList.VirtualMode = false;
normalList.SmallImageList = imageList;
for (int i = 0; i < 100; i++)
{
ListViewItem item = new ListViewItem();
item.Text = "Item " + i.ToString();
item.ImageKey = "key";
normalList.Items.Add(item);
}
ListView virtualList = new ListView();
virtualList.Columns.Add("Column");
virtualList.Dock = DockStyle.Fill;
virtualList.View = View.Details;
virtualList.VirtualMode = true;
virtualList.SmallImageList = imageList;
virtualList.RetrieveVirtualItem += (s, e) =>
{
e.Item = new ListViewItem();
e.Item.Text = "Item " + e.ItemIndex.ToString();
e.Item.ImageKey = "key";
};
virtualList.VirtualListSize = 100;
SplitContainer split = new SplitContainer();
split.Dock = DockStyle.Fill;
split.Panel1.Controls.Add(normalList);
split.Panel2.Controls.Add(virtualList);
this.Controls.Add(split);
split.SplitterDistance = split.Width / 2;
}
}