Silverlight 3 Candies – Drag & Drop

istockphoto.com

istockphoto.com

Silverlight has already come quite a long way of improvements. We have now version 3 which includes enhanced features that earlier required much more coding. Currently, some of these issues are solved and shipped together in the Silverlight library. I call them candies. They are just as easy to use and as sweet as the real ones. Today, I will show what Drag & Drop candy looks like.

Hiistoric background

From the very beginning of Silverlight, Drag & Drop feature was requested several times by the community to be implemented as part of the framework. Some people, in order to enable Drag & Drop functionality, were attaching their object events (MouseButtonDown, MouseButtonUp, and MouseMove) to the specific object and handled the position transformation in the proper method call. It also required storing some kind of singleton ( the flag which is storing information if a specific element is currently dragged). Others were making their own base classes that were implementing Drag & Drop functionality. There is even a special library on CodePlex which is made for managing Drap & Drop operations. (Unfortunately it is not supporting Silverlight 3.0) While trying to ease above examples I’ve noticed this short article about Drag & Drop in Silverlight 3.0 which advises to use Microsoft Expression Blend 3.0 to access Assets>Behaviors and set there MouseDragElementBehaviour on specific object. Well, I don’t want to use Blend to do it, I like to do it from code so I were searching further and I have found this post on Silverlight Forum.

Candy!!!

To Drag & Drop any element you want you have to do following:
  1. Add to the project two new References Microsoft.Expression.Interactions and System.Windows.Interactivity
  2. Use the code bellow in any place in your source code, for instance to move object named objectToMove (after InitializeComponent call)
Share your opinion and experience with us below or meet us on Twitter: @GOYELLO.

IT guy with head full of ideas, strongly oriented on achieving great goals in life. Currently working for Aspire Systems Poland, place where I grow.