TreeView, ListView, Drag and Drop and Command Pattern Example
<>Erik Porter asked me via an IM session whether doing drag and drop with a Tree View was difficult. I explained to him that it was actually pretty straight forward, however, if you want to get the same (or at least similar) results as say if you were using Windows Explorer, you’d have to do a bit more work since that visual behavior is not part of the components that come with the Framework.
I threw together a quick example showing how to use the drag and drop (including the highlighting effect while hovering) features between the two controls, in addition I figured I’d show how to use the Command Pattern. At this point, it’s an unfinished example in the fact that it’s not polished and some functions may not work as expected. It’s basically an extremely limited version of Windows Explorer style application. For those interested, I’ve posted it for your browsing pleasure. If you have any suggestions for improvement, please let me know. (Just keep in mind that this is a very quickly thrown together sample, so please don’t be too harsh ;-) )
Download TreeListViewDragDrop.zip
UPDATE: OK, it’s a bit more polished now. Basically this example shows how to use:
- Drag and Drop behavior between a TreeView and ListView.
- Drag and Drop hover behavior similar to Windows Explorer.
- In place editing within the TreeView and ListView.
- File and Directory I/O (Move, Rename, Delete, New).
- Command Pattern applied to menu and keyboard actions.
- An example of a technique to show Folder / SubFolder hierarchy without trying to traverse the entire tree at one time.
UPDATE: I should point out that the behavior being emulated is that which is common across all of the list views I mentioned. This example does not implement the ghosted icon behavior that some software supports. I’ve looked at several methods for doing this and there is a significant complexity surrounding it. Apparently, creating the custom mouse icon isn’t too difficult, however, having it display as a ghosted icon in the same behavior is. By this, I mean that you have to handle the feedback for every control on your form in order for the icon to stay visible. If people are interested in the links to some of these resources I’ve found, let me know and I’ll add them.