当无法找到ListViewItem的ImageKey时,是否存在使用默认映像的内置方式?
我有一个ImageList:
和一个ListViewItem
var item = new ListViewItem("Item1");
item.ImageKey = "computer.png";
默认情况下,ListView在这种情况下不显示图像。是否有可能显示"default.png“
发布于 2013-05-15 15:51:29
这应该可以做到(尽管可能有一个更优雅的方法)。您可能需要对此进行调整,以适应您填充ListView的方式。基本代码如下:
string imageKey = "DefaultKey";
string someOtherKey = "SomeOtherKey";
if (lv.SmallImageList.Images.ContainsKey("SomeOtherKey"))
{
imageKey = someOtherKey;
}
var item = new ListViewItem("Item1");
item.ImageKey = imageKey;
请注意,根据填充列表的方式,可能最好将其提取到返回适当键的函数中:
string getImageKey(string candidateKey)
{
if (lvAzureDirectory.SmallImageList.Images.ContainsKey(candidateKey))
{
return candidateKey;
}
else
{
return "DefaultKey";
}
}
https://stackoverflow.com/questions/16563890
复制相似问题