2011-02-26 188 views
4

所以,我有一個列表視圖,如問題標題所示。我有兩列設置:姓名和修改日期。這些都是在設計師加入,這裏是由設計師參考發出的代碼:爲什麼我的ListView不能正確顯示Details視圖?

// lstFiles 
this.lstFiles.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { 
this.clmName, 
this.clmDate}); 
// ... 

// clmName 
this.clmName.Text = "Name"; 
this.clmName.Width = 105; 

// clmDate 
this.clmDate.Text = "Modified"; 
this.clmDate.Width = 128; 

在設計,這個看上去很美。

列表項本身是一個ListViewItem的一個微小的子類,簡單地提取一些元數據從一個文件(在這種情況下,修改日期),並增加了一個分項目本身:

class GalleryItem : ListViewItem { 
    public string File; 
    public DateTime DateModified; 

    public GalleryItem(string file) : base(Path.GetFileNameWithoutExtension(file)) { 
     this.ImageKey = Path.GetExtension(file); 
     File = file; 
     DateModified = System.IO.File.GetLastWriteTime(file); 

     this.SubItems.Add(DateModified.ToString()); 
    } 
} 

要添加項目清單,我只是這樣做:

lstFiles.BeginUpdate(); 
lstFiles.Clear(); 

foreach (String f in files) { 
    ListViewItem lvi = new GalleryItem(f); 
    lvi.Group = lstFiles.Groups["grpFiles"]; //this varries 

    //omitted: check/add icon to list 

    lstFiles.Items.Add(lvi); 
} 

lstFiles.EndUpdate(); 

所以,這一切都爲大圖標視圖的偉大工程,等:

Large Icon View

然而,它打破了上詳細查看:

Details View

在列表項目(有一個滾動條)。如果您大致點擊紅色箭頭下的列(添加在繪畫中),則會選擇一個項目(右上方區域爲圖像預覽),但不會看到任何選定內容。

總之,我做錯了什麼?

+0

這個紅色箭頭從哪裏來?你不能只是重寫OnPaint和/或與UserPaint風格混淆。 – 2011-02-26 17:01:02

+0

請確保您閱讀了整個問題:「如果您大致點擊**紅色箭頭(添加在繪圖中)的列**」 – 2011-02-26 17:03:30

回答

7

我剛剛颳起了樣品測試:

using System; 
using System.Windows.Forms; 

static class Program 
{ 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 

     var G1 = new ListViewGroup("Group 1"); 
     var G2 = new ListViewGroup("Group 2"); 

     Application.Run(new Form { 
      Controls = { 
       new ListView { 
        Dock = DockStyle.Fill, 
        Groups = { G1, G2 }, 
        View = View.Details, 
        //Columns = { "First", "Second" }, 
        Items = { 
         new ListViewItem { Text = "One", Group = G1, SubItems = { "1" } }, 
         new ListViewItem { Text = "Two", Group = G2, SubItems = { "2" } }, 
         new ListViewItem { Text = "Three", Group = G2, SubItems = { "3" } }, 
        }, 
       }, 
      }, 
     }); 
    } 
} 

你會發現,它複製了問題。如果您取消註釋創建其工作的列的行。這表明你的專欄不存在。

雖然打字答案突然出現在我的腦海:

要調用ListView.Clear代替ListView.Items.Clear所以你在代碼中移除列。

+0

這是爲什麼適當的睡眠對於高質量代碼來說至關重要,非常感謝! – 2011-02-26 22:08:59

+0

很好的結果!錯過了。 – XIVSolutions 2011-02-27 20:58:39

0

我的理解是,您無法在詳細信息視圖中使用組。我現在不能測試這個,所以我會直接回憶。但是,嘗試在沒有組的情況下填充列表並查看會發生什麼。我強烈懷疑造成問題的是集團的一部分。

+0

我試圖根據您的建議刪除與組相關的代碼,但我仍然得到一樣的問題。 :( – 2011-02-26 16:40:17

+0

@SIVSolutions:可以在Details View模式下使用Groups – CharithJ 2011-05-22 23:49:03

0

我有Tergiver發現的完全相同的問題,但還有一個問題 - 當您添加列時,ListView必須處於View.Details模式。

相關問題