General Grey
General Grey

Reputation: 3688

How can I move an Image around the screen using DragAndDrop

I am attempting to implement DragAndDrop for an ImageControl in WPF. I add the Image to a Grid using C# codebehind.

System.Windows.Controls.Image OldMan = new System.Windows.Controls.Image();
OldMan.Height=30;
OldMan.Width=30;
OldMan.Name="OldMan";
OldMan.Margin=new Thickness(100,100,0,0);
OldMan.HorizontalAlignment=System.Windows.HorizontalAlignment.Left;
OldMan.Stretch = Stretch.Fill;
OldMan.VerticalAlignment = System.Windows.VerticalAlignment.Top;
OldMan.Source= ConvertBitmap(Properties.Resources.Old1);
OldMan.MouseDown += new MouseButtonEventHandler(OldMan_MouseDown);
//PW is the name of my Grid
PW.Children.Add(OldMan);
PW.RegisterName(OldMan.Name, OldMan);

This adds the Image to the grid, and hooks onto the MouseDown event

in the MouseDown event

void OldMan_MouseDown(object sender, MouseButtonEventArgs e)
{
    System.Windows.Controls.Image img = (System.Windows.Controls.Image)sender;
    DoDragDrop(....   //this doesn't exist so obviously I am missing something
}

So I don't have the DoDragDrop option, so what are my options

Upvotes: 0

Views: 434

Answers (1)

Tim
Tim

Reputation: 15237

There's a writeup on WPF Tutorials that might be useful:

http://wpftutorial.net/DragAndDrop.html

The basics of it are that when they press the mouse down, you record the start point. When they then move the mouse (without having released the mouse button) you move the object the same amount they've moved the mouse.

Upvotes: 2

Related Questions