2012-04-19 92 views
2

加載圖標進入我的列表視圖時出現問題。我可以讓圖像在大視野下工作,但不能詳細說明,不太確定我做錯了什麼。使用View.Details向ImageList填充ListView

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; 
} 

回答

5

變化

listView1.StateImageList = this.imageList1; 

listView1.SmallImageList = this.imageList1; 

,並確保您正在設置ImageIndex,或ImageKey每個列表項的屬性。

listItem.ImageIndex = 0; // or, 
listItem.ImageKey = "myImage"; 
+0

啊不適用於我:(可能是與我正在做的其他事情的問題:P – cheeseman 2012-04-19 13:26:06

+0

確保你正在設置圖像鍵/索引,更新了答案。 – 2012-04-19 13:29:23

+0

非常好,謝謝!不得不取消註釋我的方法,它首先運行,再次感謝! – cheeseman 2012-04-19 13:32:52

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); 
     } 

您甚至可以添加第二列,而 「增加」 的文件名。

+0

字符串數組包含的路徑的具體名單,所以我不能用這樣的一個目錄:P用你的方法的其他問題是我需要這個了listView.View = View.Detail;此外,我相信我已經看到你在搜索類似的問題時發佈在stackoverflow上的代碼,你應該參考你從它得到的地方:P – cheeseman 2012-04-19 13:24:26