切换视图模式后,ListView控件不显示图片 [英] ListView does not show images after switching view mode
问题描述
当我加入项目(S)的的ListView
在任何其他比 LargeIcon
视图模式下,的ListView
停止显示来自 LargeImageList
当它切换回 LargeIcon
。这种情况一直持续到新的项目加入到 LargeIcon
模式的的ListView
。
When I add item(s) to the ListView
in any other than LargeIcon
view mode, the ListView
stops showing the images from LargeImageList
when it is switched back to LargeIcon
. This situation lasts until new item is added to the ListView
in the LargeIcon
mode.
所以,按以下顺序说明了这个问题:
So the following sequence illustrates the problem:
- 创建
的ListView
,添加列,设置查看
到详细信息
- 创建
的ImageList
,设置IMAGESIZE
,将其分配给ListView.LargeImageList
- 创建新的
的ListViewItem
,设置其ImageKey
- 创建新的形象,它与给定的密钥添加到
的ImageList
- 添加
的ListViewItem
到的ListView
- 切换
的ListView
模式LargeIcon
- 无图像显示
- create
ListView
, add column, setView
toDetails
- create
ImageList
, setImageSize
, assign it toListView.LargeImageList
- create new
ListViewItem
, set itsImageKey
- create new image, add it to the
ImageList
with given key - add the
ListViewItem
to theListView
- switch the
ListView
mode toLargeIcon
- no images are shown
- 所有图像都显示为预期
有什么意思,我仍然失踪?
我曾尝试以下操作:
- 无效
的ListView
- 重新分配
LargeImageList
前/添加项目之后(甚至通过空
)
- Invalidate the
ListView
- Re-assign the
LargeImageList
before/after adding the item (even throughnull
)
测试code对于那些谁喜欢话多的:
The test code for those who like it more than words:
public partial class Form1 : Form
{
int counter = 0;
ImageList iList = new ImageList();
private string GetNewKey()
{
return counter++.ToString();
}
private Image GetNewImage(Size size)
{
var bmp = new Bitmap(size.Width, size.Height);
using (var gra = Graphics.FromImage(bmp))
{
var rnd = new Random();
var lines = rnd.Next(1000);
for (int l = 0; l < lines; ++l)
{
var pen = new Pen(Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256)));
var p1 = new Point(rnd.Next(size.Width), rnd.Next(size.Height));
var p2 = new Point(rnd.Next(size.Width), rnd.Next(size.Height));
gra.DrawLine(pen, p1, p2);
}
}
return bmp;
}
public Form1()
{
InitializeComponent();
iList.ImageSize = new Size(100, 100);
listView.LargeImageList = iList;
listView.Columns.Add("name");
}
private void buttonAdd_Click(object sender, EventArgs e)
{
var key = GetNewKey();
var lvi = new ListViewItem()
{
Name = key,
Text = "blabla",
ImageKey = key,
};
iList.Images.Add(key, GetNewImage(new Size(100, 100)));
listView.Items.Add(lvi);
}
private void buttonClear_Click(object sender, EventArgs e)
{
listView.Items.Clear();
}
private void buttonLarge_Click(object sender, EventArgs e)
{
listView.View = View.LargeIcon;
}
private void buttonDetails_Click(object sender, EventArgs e)
{
listView.View = View.Details;
}
}
编辑:
对于任何人谁也遭受了同样的问题。一些实验后,至少有愚蠢的可怜人的解决方法:
For anyone who would suffer the same problem. After some experiments, there is at least stupid poor man's workaround:
修改的ImageList
的的ListView
不知何故检测其变化并重新载入图片为 LargeIcon
模式。这些问题是如何它检测到的变化和为什么它忽略的ImageList
模式改变后...
Modify the ImageList
, the ListView
somehow detects its change and reloads the images for LargeIcon
mode. The questions are how it detects the change and why does it ignore the ImageList
after mode change...
private void FixIt()
{
// Trigger a reload of the ListView.LargeImageList
if (listView.View == View.LargeIcon)
{
var key = "Dummy image to be deleted right after its insertion...";
iList.Images.Add(key, new Bitmap(1, 1));
iList.Images.RemoveByKey(key);
}
}
编辑#2:
我还发现了其他一些有趣的功能的ListView
和相关的组件有。您可能要检查他们的答案<一href="http://stackoverflow.com/questions/4097912/why-does-subitems-clear-also-delete-the-name-attribute">question 4097912 和<一href="http://stackoverflow.com/questions/23059678/imagelist-strange-behaviour-ater-changing-imagesize">question 23059678
EDIT #2:
I have also discovered some other funny features the ListView
and associated components have. You might want to check them in answers of question 4097912 and question 23059678
推荐答案
解决你的问题
您可以通过期运用避免这种的ImageIndex
而不是 ImageKey
来连接你的的ListView
与的ImageList
。因此,在你的 buttonAdd_Click
事件中使用:
You could avoid this by useing ImageIndex
instead of ImageKey
to connect your ListView
with the ImageList
. So in your buttonAdd_Click
event use:
var lvi = new ListViewItem()
{
Name = key,
Text = "blabla",
//ImageKey = key,
//Use ImageIndex and don't set both
ImageIndex= Convert.ToInt32(key) //you could just use count++
};
这背后的问题,究其原因:
这背后的原因是我也不清楚,但我认为这可能是一个错误,从详细信息
更改为 LargeIcon $时, C $ C>,它只是在默认检查
的ImageIndex
如果设置 ImageKey
的的ImageIndex
将被设置为 1
。或者可能是这是由设计,我不知道(见下文 ImageKey
部分),因为你没有一个 SmallImageList
,所以改为 LargeIcon
视图,的ImageIndex
是空$时, C $ C>或
1
和 ImageKey
被忽略。
The reason behind this is not clear to me, but I assume this may be a bug that when changing from Details
to LargeIcon
, it only checks ImageIndex
in default and if you set ImageKey
the ImageIndex
will be set to -1
. Or may be this is by design, I don't know (see ImageKey
section below), since you don't have a SmallImageList
, so when changing to LargeIcon
view, ImageIndex
is null
or -1
and the ImageKey
is ignored.
关于<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.imageindex%28v=vs.110%29.aspx"相对=nofollow> ListViewItem.ImageIndex物业
About ListViewItem.ImageIndex Property
在ImageKey和图像索引的属性是相互排斥的,这意味着如果已设置,另一个被忽略。此外,如果你设置ImageKey属性,ImageIndex属性被自动设置为-1。另外,如果设置了ImageIndex属性,该ImageKey被自动设置为空字符串()。
The ImageKey and ImageIndex properties are mutually exclusive, meaning if one is set, the other is ignored. Furthermore, if you set the ImageKey property, the ImageIndex property is automatically set to -1. Alternatively, if you set the ImageIndex property, the ImageKey is automatically set to an empty string ("").
关于<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.imagekey%28v=vs.110%29.aspx"相对=nofollow> ListViewItem.ImageKey物业
About ListViewItem.ImageKey Property
如果您正在使用多个图像列表,为小型和大型图标视图,具有ListView控件,你应该把大大小小的版本的形象在他们各自的图像列表相同的索引位置。的的当视图之间切换时,图像的一个列表中的索引位置被用来定位图像中的其他列表,而不管指定的密钥值的的强>
If you are using multiple image lists, for small and large icon view, with a ListView control, you should place small and large versions of the image at the same index location in their respective image lists. When switching between views, the index location of the image in one list is used to locate the image in the other list, regardless of the key value specified.
这能以某种方式进行验证:
And this can somehow be verified:
- 在使用现有的code(使用
ImageKey
) - 设置
的ImageIndex
任何项
的的ListView
在buttonLarge_Click
事件处理程序会告诉你该项目的形象。 - 设置
ImageKey
任何项目
在buttonLarge_Click
事件处理程序将不会显示,纺织机械展览会的形象。
- using your existing code (use
ImageKey
) - set
ImageIndex
for anyItem
in yourlistView
within thebuttonLarge_Click
event handler will show you that item's image. - set
ImageKey
for anyItem
within thebuttonLarge_Click
event handler will not show that itme's image.
例如:
private void buttonLarge_Click(object sender, EventArgs e)
{
listView.View = View.LargeIcon;
//Set ImageIndex of Item 0 you could see its Icon.
listView.Items[0].ImageIndex= 0 ;
//set ImageKey will change nothing
//listView.Items[0].ImageKey= "0" ;
}
这篇关于切换视图模式后,ListView控件不显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!