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.
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.
To Drag & Drop any element you want you have to do following:
Add to the project two new References Microsoft.Expression.Interactions and System.Windows.Interactivity
Use the code bellow in any place in your source code, for instance to move object named objectToMove (after InitializeComponent call)
We process cookies and make them available to Google Analytics (a service provided by Google, Inc.) to improve the performance of the website, to learn your preferences about using it and to tailor it to your needs. The data will be anonymised before being transmitted. If you do not agree to this, you may disable cookies in your browser. If you do not change your browser settings, you accept the fact that it saves cookies.