2012-02-21 36 views
0

ListView處於虛擬模式,你負責饋送ListView一個ListItem對應於索引n當它要求通過OnRetrieveItem事件。如何在虛擬模式下對ListView的底層列表進行排序時進行排序?

我根據我自己的規則排序我的名單,並告訴列表視圖重繪:

listView1.Invalidate(); 

這是好得很。

除用戶選擇了一些項目。現在,當樹重新繪製時,不同項目被選中。

什麼是技術來排序SelectedIndices

但是,如果我整理我自己個人的名單

回答

1

您需要在他們的新指數,其存儲選擇的對象,排序,找到對象,並重新選擇了。

的代碼可能看起來像這樣(優化它,你認爲合適):

void listView1_ColumnClick(object sender, ColumnClickEventArgs args) 
{ 
    // Store the selected objects 
    List<MyDataObject> selectedObjects = new List<MyDataObject>(); 
    foreach (int index in listView1.SelectedIndices) 
    { 
     selectedObjects.Add(m_MyDataObjectsColl[index]); 
    } 

    // Clear all selected indices 
    listView1.SelectedIndices.Clear(); 

    // Sort the list 
    SortListView(listView1, args); 

    // Reselect the objects according to their new indices 
    foreach (MyDataObject selectedObject in selectedObjects) 
    { 
     int index = m_MyDataObjectsColl.FindIndex(
       delegate(MyDataObject obj) { return obj == selectedObject; } 
      ); 
     listView1.SelectedIndices.Add(index); 
    } 
}