在我的列表视图中加载图标时出现问题。我可以让图像在大视图中工作,但不能在细节上工作,不太确定我做错了什么。
private void CreateList()
{
listView1.View = View.Details;
listView1.Columns.Add("Icon", -2, HorizontalAlignment.Center);
listView1.Columns.Add("Name", -2, HorizontalAlignment.Left);
imageList1.ImageSize = new Size(32, 32);
for (int i = 0; i < subKeys.Length; i++)
{
if (subKeys[i].Contains("App"))
{
imagePath = subKeys[i];
if (System.IO.File.Exists(imagePath))
{
imageList1.Images.Add(Image.FromFile(imagePath));
}
numberOfImages++;
}
}
listView1.StateImageList = this.imageList1;
}
答案 0 :(得分:5)
更改
listView1.StateImageList = this.imageList1;
要
listView1.SmallImageList = this.imageList1;
并确保为每个ListItem设置ImageIndex
或ImageKey
属性。
listItem.ImageIndex = 0; // or,
listItem.ImageKey = "myImage";
答案 1 :(得分:0)
试试这段代码:
DirectoryInfo dir = new DirectoryInfo(@"c:\myPicutures"); //change and get your folder
foreach (FileInfo file in dir.GetFiles())
{
try
{
this.imageList1.Images.Add(Image.FromFile(file.FullName));
}
catch{
Console.WriteLine("This is not an image file");
}
}
this.listView1.View = View.LargeIcon;
this.imageList1.ImageSize = new Size(32, 32);
this.listView1.LargeImageList = this.imageList1;
//or
//this.listView1.View = View.SmallIcon;
//this.listView1.SmallImageList = this.imageList1;
for (int j = 0; j < this.imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
this.listView1.Items.Add(item);
}
您甚至可以添加第二列,并“添加”文件名。