Silverlight & Blend Animation Design Series eight: Drag-and-drop (Drag-drop) operations and drag-and-drop behavior

Source: Internet
Author: User
Tags expression silverlight

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.