0

当 aListView处于虚拟模式时,您负责在它通过事件询问时提供与 index 对应的ListViewa 。ListItemnOnRetrieveItem

我根据自己的规则对列表进行排序,并告诉列表视图重新绘制:

listView1.Invalidate();

这很好,花花公子。

除非用户选择了某些项目。现在,当树重新绘制时,会选择不同的项目。

排序的技术SelectedIndices是什么?

但是,如果我对自己的个人列表进行排序

4

1 回答 1

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 );
    }
}
于 2012-02-21T23:33:00.700 回答