In Silverlight itself does not provide the implementation of drag-and-drop functions, to implement the drag-and-drop function with its event support (MouseLeftButtonDown, MouseLeftButtonUp and MouseMove) to complete, in practical applications we can Encapsulates a drag-and-drop operation as a behavior by using the behavior (Behavior) attribute, which achieves the effect of code reuse. In Blend, a drag-and-drop operation is provided directly under the Microsoft.Expression.Interactions.dll Microsoft.Expression.Interactivity.Layout namespace.
The drag-and-drop operations in Silverlight are usually implemented using event-driven dynamic positioning of the object's coordinates, first to see how to implement drag-and-drop operations in Silverlight programmatically through code, the following block of code:
private void Onmouseleftbuttondown (object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
MousePosition = E.getposition (null);
Ismousecaptured = true;
Element. CaptureMouse ();
Element. Cursor = Cursors.hand;
}
private void Onmouseleftbuttonup (object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
Ismousecaptured = false;
Element. Releasemousecapture ();
mouseposition.x = MOUSEPOSITION.Y = 0;
Element. Cursor = null;
}
private void OnMouseMove (object sender, MouseEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
if (ismousecaptured)
{
Double Y = e.getposition (null). Y-MOUSEPOSITION.Y;
Double X = e.getposition (null). x-mouseposition.x;
x = x + (double) element. GetValue (Canvas.leftproperty);
y = y + (double) element. GetValue (Canvas.topproperty);
Element. SetValue (Canvas.leftproperty, X);
Element. SetValue (Canvas.topproperty, Y);
MousePosition = E.getposition (null);
}
}
As defined in the three methods to achieve the object drag-and-drop algorithm, the actual application only need to drag and drop the object to be moved to add MouseLeftButtonDown, MouseLeftButtonUp and MouseMove event processing on the line. The following sample code:
attachedElement.MouseLeftButtonDown += (s, e) => OnMouseLeftButtonDown(s, e);
attachedElement.MouseLeftButtonUp += (s, e) => OnMouseLeftButtonUp(s, e);
attachedElement.MouseMove += (s, e) => OnMouseMove(s, e);
As a general rule, we will encapsulate the implementation of these related methods into a base class for reuse purposes, but this article does not recommend using a base class to encapsulate drag-and-drop behavior because Silverlight has an attribute-behaviors specifically designed to handle object behavior. The basic framework for behavior is provided under the System.Windows.Interactivity namespace in Silverlight, and we can expand our behavior freely to achieve our different needs. After installing blend, you can find Microsoft.Expression.Interactivity.dll this library under the installation directory, which provides some of the more common set behavior extensions that open the asset panel in Blend through the window--assets. Select the behavior asset to see the extended behavior provided in Silverlight 3, as shown in the following illustration: