MouseDragElementBehavior with Silverlight 3

Some great new functionality. One of my favorites is MouseDragElementBehavior. We've all written drag and drop functionality over the years. but with Expression Blend and Silverlight 3 it's easier than ever.

To start, lets create a new Silverlight application, go ahead and select create new web site with the solution.

Once it loads, add a class named MyDragEvent.cs, using Microsoft.Expression.Interactivity.Layout in your class let add.

Change your class to inherit from MouseDragElementBehavior.
public class MyDragEvent : MouseDragElementBehavior

Add a constructor and in the constructor add an event to handle the mouse release event.
base.DragFinished += new MouseEventHandler(MyDragEvent_DragFinished);

Put in the code to handle the event, and lets add a popup in there just because there are not already enough popups in the world.
void MyDragEvent_DragFinished(object sender, MouseEventArgs e)
{
  
HtmlPage.Window.Alert("Ground control, this is major Tom.");
}

Now the fun part!!!

Save the solution and open it in Expression Blend.

In Blend, create a rectangle and change the background to a Gradient, whichever colors you like.

After that you select the 'Behaviors' Item in the Assets Tab (Like So)



Next you should notice that the event we created in our class is available in the listbox of Behaviors. Now all you have to do is drag 'MyDragEvent' on to the rectangle you created and then save your work. Now close Blend, go back to your solutions and run it. You should be able to drag until your wrist hurts.

 

Happy Coding!


Posted by: Rick Sammons
Posted on: 7/15/2009 at 7:04 AM
Tags: , ,
Categories: Silverlight 3
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed