我有一个 System.Windows.Forms.ListView。在正常(非虚拟)操作下,ListViewItems 的图像正确显示。但是,当我尝试将 ListView 转换为虚拟模式时,项目不再显示其图像。其他所有内容(文本、索引等)都按预期显示。
ListView 处于详细信息模式。SmallImageList 设置正确。每个 ListViewItem 都正确设置了其 ImageKey。ImageList 已正确填充。
为什么图像没有显示在虚拟 ListView 上?我可以做些什么来确保图像按预期显示?
编辑:请参阅以下代码作为演示我遇到的问题的示例。
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;
}
}