Last week I needed to include drag / drop capabilities on a Tree View on a WPF application.
As usual I started cutting the code required to give me the required capabilities. After some time it became obvious that Drag Drop functionality in the MVVM world was more complicated than what I first thought. All my code was residing in code behind instead of the view model?
I took some time out to search the internet to find out how others have resolved this issue. There are many solutions to found.
I did however discover one solution that I thought was very well done. Steven Kirk (back in 2009) has created a library that he calls gong-wpf-dragdrop.
His web site says:
The GongSolutions.Wpf.DragDrop library is a drag'n'drop framework for WPF. It has the following features:
- Works with MVVM : the logic for the drag and drop can be placed in a ViewModel. No code needs to be placed in codebehind, instead attached properties are used to bind to a drag handler/drop handler in a ViewModel.
- Works with multiple selections.
- Can drag data within the same control to re-order, or between controls.
- Works with TreeViews.
- Can insert an item into a collection, or drop one item onto another.
- Can display Adorners to give the user visual feedback of the operation in progress.
- Has sensible defaults so that you have to write less code for common operations
I found this library very easy to use and would recommend you consider this solution if you want to add drag drop capabilities to your WPF application.