views:

516

answers:

2

Hi,

I put several shapes (like Ellipse and Rectangle) on a Canvas. Now, I want user to be able to drag & drop these shapes. Is there some predefined functionality that I can use, or I should implement the drag & drop myself using the mouse events ?

Thanks !

A: 

I believe you will need to do this yourself, using the mouse events and the visual tree. Here is an article I believe will help - link text. If not I have some sample code that I can post later this evening.

HTH

Wade73
A: 

Handling the mouse events and implementing drag and drop yourself will certainly work, but depending on what you are trying to do you may be able to leverage Expression Blend behaviors. The Microsoft.Expression.Interactions DLL includes some useful basic behaviors, triggers, and actions to be used in Silverlight and WPF.

There is a MouseDragElementBehavior that implements a basic drag and drop functionality for an element, which should work regardless of your layout container (so you wouldn't be constrained to a Canvas). You can drop this behavior onto an element using Blend, or define it directly in XAML if you would like:

<Rectangle Fill="Red" Stroke="Black" HorizontalAlignment="Left" Width="100" Height="100">
    <i:Interaction.Behaviors>
        <il:MouseDragElementBehavior/>
    </i:Interaction.Behaviors>
</Rectangle>

Your project will have to reference both System.Windows.Interactivity.dll and Microsoft.Expression.Interactions.dll to use this behavior.

EDIT (to show attaching this behavior in C# code-behind):

Rectangle rect = new Rectangle();
rect.Fill = new SolidColorBrush(Colors.Red);
rect.Width = 100;
rect.Height = 100;

MouseDragElementBehavior dragBehavior = new MouseDragElementBehavior();
dragBehavior.Attach(rect);

Remember to include the Microsoft.Expression.Interactivity.Layout namespace with your using statements.

Dan Auclair
Hi,I'm not really familiar with Expression Blend, but it sounds interesting.I'm doing some kind of program that enable users to draw geometric problems (Circles, Triangles, Bisectors etc).Since the Shapes are created dynamically by the user, I guess that I need to set the MouseDragElementBehavior in C# rather than XAML, right ?Could you please provide a C# sample code of doing that ?How should I reference to System.Windows.Interactivity.dll and Microsoft.Expression.Interactions.dll ?Thanks !
Dan Auclair
Sounds interesting... I downloaded "Microsoft Expression Blend 3 SDK" but when I started to install it, I got this message: ".Net version 3.5 or newer run-time components must be installed before installing Microsoft Expression Blend 3 SDK. Please try installing again after correcting the issue.". However, I see that "Microsoft .Net Framework 3.5" is installed on my computer. Isn't this enough ?
Not sure... the download requirements say .Net 3.5 SP1. Are you sure you have SP1 installed?
Dan Auclair
Yes.Here is all .NET stuff that is installed on my computer:* Microsoft .NET Compact Framework 2.0 SP2* Microsoft .NET Compact Framework 3.5* Microsoft .NET Framework 2.0 Service Pack 1* Microsoft .NET Framework 3.0 Service Pack 1* Microsoft .NET Framework 3.5Is anything missing ?Could you point me to the download page of all missing stuff ?Thanks !
OK, finally I installed "Microsoft Expression Blend 3 SDK".How should I reference to System.Windows.Interactivity.dll and Microsoft.Expression.Interactions.dll in my project ? Thanks !
Same way you reference any other assembly. Installing the SDK should have added them to the .NET tab in the Add Reference dialog. If they aren't there, you can browse to %PROGRAM FILES%\Microsoft SDKs\Expression\Blend 3\Interactivity\Libraries\WPF and grab them from there.
Dan Auclair
Wow ! It works and looks so amazing !! Is this true that I must create a new MouseDragElementBehavior object for each shape ? In other words, if I have an array of Shapes, I must have a parallel array of MouseDragElementBehavior ? I tried also to look for some documentation/examples on MouseDragElementBehavior and other available behaviours, but could not find anything helpful. Could you point me please to some references ? Thanks !
I honestly haven't ever used the MouseDragElementBehavior besides just playing around, but if you are attaching the behaviors in code-behind I would assume you need one instance per element. There are plenty of posts out there on the new Behaviors, Triggers, and Actions in the Blend assemblies, here is one I got started with: http://blogs.msdn.com/dphill/archive/2009/09/25/blend-behaviors.aspxIf you want to do more advanced things you will have to start creating your own behaviors and actions. Hope that helps!
Dan Auclair