Grouping and sorting Folders and Files in a ListView
I'd like to display fo开发者_运维问答lders and files in a ListView the same way they are in the windows explorer i.e. folders first, then the files, and both groups sorted alphabetically.
I thought at first about using 2 ListViewGroup (one for Folders and one for files), but not only I can't hide the group header, it's not active when the ListView is in List mode.
Another solution would be to keep 2 underlying lists (one for folders and one for files) and populate the ListView from the 2 lists (first the folders and then the files). But this seems a bit clumsy as I'd have to sort my 2 lists and refresh the ListView content every time the user sorts the ListView.
Can anybody suggest solution to this issue? I feel like there's a simple answer and/or that I've missed something in the ListView control...
OK after a bit more searching here's how I've implemented this.
I used the custom sort functionality in the list view (see how to on http://support.microsoft.com/kb/319401).
The only change is in the Compare function of the ListViewColumnSorter. If both X and Y items are of the same type (Folder or File) I return the "normal" result based on the item name sort. And if not, I return -1 if X is a folder and 1 if not.
That way folders always come first and both folders and files are sorted alphabetically
Each list view item gets is identified as a folder or file by its Tag property set when the items are added to the list.
One way to handle this is to use a TreeView for the folders (on the left), and a Listview for the files (on the right). Whenever the selection changes in the Treeview, you can update the file names in the Listview.
精彩评论