ListView<-> ImageList将不同步 [英] ListView <--> ImageList Will Not Sync
问题描述
我正在使用ListView加载和显示图像文件的缩略图.我用于每个ListViewItem的名称"或键是完全限定的文件名.
I am using a ListView to load and display thumbnails for image files. The "Name" or key that I'm using for each ListViewItem is the fully-qualified file name.
我还创建了一个ImageList,该ImageList使用相同的键加载这些文件的缩略图.我已将此ImageList分配为ListView的"SmallImageList"属性.
I have also created an ImageList which loads thumbnails of those files using the same key. I've assigned this ImageList as the "SmallImageList" property of the ListView.
添加和删除文件时,我会通过键从ListView.Items和ImageList.Images集合中添加和删除它们.
When I add and remove files, I add and remove them by key from both the ListView.Items and the ImageList.Images collections.
在加载许多图像时,所有这些都可以正常工作.但是,当我尝试删除给定键时,ListView控件不再能正确显示缩略图.
All of this works fine when loading many images. However, when I try deleting a given key, the ListView control no longer displays the thumbnails properly.
删除项目前后:
当我在调试过程中分析内存中的两个集合数组时,键完美对齐.
When I analyze both collection arrays in memory during debugging, the keys line up perfectly.
使用的代码:
// Add the images from an array of paths
foreach (string xFile in files)
{
thumbnails_imageList.Images.Add(xFile, images[xFile]);
files_lst.Items.Add(xFile, Path.GetFileNameWithoutExtension(xFile), xFile);
}
// Delete the selected key(s)
foreach (ListViewItem xItem in files_lst.SelectedItems)
{
files_lst.Items.Remove(xItem);
thumbnails_imageList.Images.RemoveByKey(xItem.Name);
}
推荐答案
发生的事情很清楚,如果您将ListViewItem
绑定到图像索引5
,并且删除了位置4
的图像,则这5个移位减小到4,并且该项目保留对5
的引用,因此不会显示任何图像.
what happens is clear, if you have a ListViewItem
bound to image index 5
and you delete the image in position 4
, the 5 shifts down to 4 and the item keeps a reference to the 5
so does not show any image.
我认为在删除选定的ListView项时,不应从ImageList
中删除图像.
I think you should not remove the images from the ImageList
when you remove the selected ListView items.
这篇关于ListView<-> ImageList将不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!