0

我有一个 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;
    }
}
4

2 回答 2

3

I know this an old question, but for sake of someone looking for this:

In Virtual mode, item.ImageKey will not work. Use item.ImageIndex instead. ImageList.ImageCollection has a IndexOfKey() method to convert the key into index.

    virtualList.RetrieveVirtualItem += (s, e) =>
    {
        e.Item = new ListViewItem();
        e.Item.Text = "Item " + e.ItemIndex.ToString();
        e.Item.ImageIndex = imagelist.Images.IndexOfKey("key");
    };
于 2018-04-15T16:29:56.067 回答
1

How can this work at all with ListView controls and ImageList being local variables of the c'tor? Does it work any better if they are member of the VirtualListViewExample class ?

UPDATE: My suggestion above is plain wrong. See duplicate question for a workaround to this issue.

于 2013-04-08T09:58:00.117 回答