Dragging and dropping pieces of information is fun. Sometimes I do it too – maybe once a week, maybe twice. The point is that you can do it in WPF, and as the title says: within and across applications.
Let’s see a fairly basic example: drag something and drop it to another control. First we need to handle the MouseDown event on the source control (you must hold down the mouse, move it to another place, and release it there). We need a method there:
private void source_MouseDown(….)
Label lab = (Label) sender;
DragDrop.DoDragDrop(lab, lab.Content, DragDropEffects.Copy);
Then you need to handle the target’s drop event, like this:
private void target_Drop(object sender, DragEventArgs e)
((Label)sender).Content = e.Data.GetData(DataFormats.Text);
These code samples were from the book Pro WPF 4 in C# 2010.
Of course you could improve this drag and drop experience to check whether or not data is present before “dropping” it on the target. There’s an event just for that, and it’s called DragEnter. A perfect candidate to check things like the format and the availability of the data to be dropped.
If you’d like to drag and drop across applications, then I have a nice link for you. It’s a little code written by Scott Garland who seemed to be a little upset when he tried to do the thing. Anyway, don’t give up the hope – you need a class implementing ISerializable or IDataObject to begin with, and the rest will be simple…