从ListView中删除带有图像的ListViewItem [英] Removing ListViewItem with image from ListView
问题描述
我正在尝试在程序中动态更改ListView
.每个项目都有一个ImageKey
,我为它们使用SmallImageList
.
I'm trying to dynamically change a ListView
in my program. Every item has an ImageKey
and I use a SmallImageList
for them.
问题是,每当我删除项目时,此问题中提到的问题就会出现:
The problem is whenever I delete an item, the problem which was mentioned in this question appears:
删除项目前后:
Before and after deleting an item:
使用的代码:
// 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);
}
该问题的答案(建议不要从ImageList
中删除图像)不符合我的要求,因为删除后我添加了具有相同ImageKey
的项目,因此,在其中添加了多个Image
SmallImageList.Images
获得相同的ImageKey
,因此图像变得不一致.答案也忽略了明显的内存泄漏.
The answer in the question (which recommends not to remove images from the ImageList
) doesn't meet my requirements because I add items with the same ImageKey
after removing, so, more than one Image
s in SmallImageList.Images
get the same ImageKey
, therefore images become inconsistent. Also the answer ignores the obvious memory leak.
推荐答案
不幸的是,从ImageList
中删除Image
确实导致Items
的索引向上移动.这意味着Keys
在内部不再使用,而是在添加或设置时映射到索引,然后不再保持完整.
Unfortunately the removal of an Image
from the ImageList
indeed causes the indices of the Items
to move up. This means that internally the Keys
are no longer used but are mapped to the indices when adding or setting and then no longer kept intact.
要解决此问题,您可以..:
So to workaround you can either..:
-
将所有
Images
保留在ImageList
中,并使用不必要的Images
.Image
在256x256像素x 4字节的情况下只能有256k,因此内存浪费不会太大. (请注意,不会浪费GDI +资源,因为ImageList
不会为其Images
创建任何句柄.)但是如果添加/删除足够大的ImageList
大小,可能会成为问题.
Keep all
Images
in theImageList
and live with the unnecessaryImages
. At 256x256pixels x 4 byte anImage
can have only 256k, so the memory waste will not be so big. (Note that no GDI+ resources are wasted, since anImageList
doesn't create any handles for itsImages
.) But given enough adding/removing the growing size of theImageList
may become a problem..
或者您可以通过存储和重置ImageKeys
来解决此问题.
Or you can work around by storing and resetting the ImageKeys
.
这里是一个例子:
private void DeleteButton_Click(object sender, EventArgs e)
{
foreach (ListViewItem xItem in listView1.SelectedItems)
{
// maybe insert the loop below here (*)
listView1.Items.Remove(xItem);
// only delete when no longer in use:
string key = xItem.ImageKey;
if (listView1.Items.Cast<ListViewItem>().Count(x => x.ImageKey == key) == 0)
imageList1.Images.RemoveByKey(key);
}
// after deletions, restore the ImageKeys
// maybe add a check for Tag == null
foreach (ListViewItem xItem in listView1.Items)
xItem.ImageKey = xItem.Tag.ToString();
}
为此,您需要存储正确的密钥字符串.我选择在IListViewItem.Tag
属性中这样做.您可以在添加Items
时或在删除之前执行此操作:
For this to work you need to store the correct key strings. I chose to do so in the IListViewItem.Tag
property. You can do it when adding the Items
or right before the deleting:
foreach (ListViewItem xItem in listView1.Items)
xItem.Tag = xItem.ImageKey; // (*)
这篇关于从ListView中删除带有图像的ListViewItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!