Implementing the drag and drop feature
When you want to provide a rich experience to the user, you may want to use the dragging and dropping feature. You may also want to add a drag and drop feature in your application to access a local resource to upload it to the server.
In this recipe, we will learn the basics of drag and drop implementation in WPF by using a simple example.
Getting ready
Open Visual Studio and create a new WPF application named CH03.DragAndDropDemo
.
How to do it...
Let's perform the following steps to create a few elements inside a window and provide the option to drag and drop from one panel to the other:
- First, open
MainWindow.xaml
and replace the existingGrid
with aStackPanel
. Set itsOrientation
property toHorizontal
. - Add two
WrapPanel
inside it and set theirWidth
,Margin
,ItemHeight
, andItemWidth
properties. - Give a name to both panels. Let's name the first wrap panel
sourcePanel
and the second wrap paneltargetPanel
. We will be using these name later from the code...