Click here to Skip to main content
Click here to Skip to main content

Drag and Drop WPF Controls

, 27 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip is about how to make our WPF controls moveable at runtime

Introduction

I was looking for a solution to make a Form Generator application with WPF. But my big problem was "How can I move my controls by user's mouse events on the Grid or other container controls".

I've searched a lot on the internet, but none of my search results helped me. Finally, I found how Canvas control and its properties can be helpful. And with using Canvas control, I created my own FormGenerator app.

Using the Code

As I said, to be able to move an object with mouse or other input devices at run time, we can use a Canvas control. When you add a control (like Button) to a Canvas, this control gets some properties that all of them are depending on the Canvas control. Properties like Canvas.Left and Canvas.Right.

With these properties, we can move an object (control) where we want on its parent (Canvas). To do this, we should have three events: MouseLeftButtonDown, MouseMove, and MouseLeftButtonUp. In some components, the MouseLeftButtonDown doesn't fire because of that, I used the PreviewMouseLeftButtonDown.

The MouseLeftButtonDown event code is like this:

private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{     
    //In this event, we get current mouse position on the control to use it in the MouseMove event.
    FirstXPos = e.GetPosition(sender as Control).X;
    FirstYPos = e.GetPosition(sender as Control).Y;
    FirstArrowXPos = e.GetPosition((sender as Control).Parent as Control).X - FirstXPos;
    FirstArrowYPos = e.GetPosition((sender as Control).Parent as Control).Y - FirstYPos;
    MovingObject = sender;
}

The MouseLeftButtonUp event code is like this:

void PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{  
    MovingObject = null;
}

And our main event, "MouseMove", is like this:

private void MouseMove(object sender, MouseEventArgs e)
{
    if (e.LeftButton == MouseButtonState.Pressed)
    {
        (MovingObject as FrameworkElement).SetValue(Canvas.LeftProperty,
             e.GetPosition((MovingObject as FrameworkElement).Parent as FrameworkElement).X - FirstXPos);

        (MovingObject as FrameworkElement).SetValue(Canvas.TopProperty,
             e.GetPosition((MovingObject as FrameworkElement).Parent as FrameworkElement).Y - FirstYPos);
    }
}

Finally

I hope this tip helps you to solve your possibility problems with drag and drop in WPF.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

S.A.Esmaeily
Software Developer (Senior) Keyhan Iranian
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I'm addict of developing softwares and designing solutions for projects.
My favorite work in programming is designing Components and user controls.

Comments and Discussions

 
BugMouse slipping off some Contents PinmemberMember 1060332617-Feb-14 8:09 
GeneralRe: Mouse slipping off some Contents [modified] PinprofessionalS.A.Esmaeily17-Feb-14 20:27 
QuestionUsing Point Pinmemberheulendoch18-Nov-13 3:01 
SuggestionNice approach PinmemberBrunoH7922-Oct-13 0:14 
GeneralRe: Nice approach PinmemberS.A.Esmaeily22-Oct-13 20:45 
SuggestionMy vote of 5 PinmemberMehdiNaseri2-Aug-13 16:58 
GeneralRe: My vote of 5 [modified] PinmemberS.A.Esmaeily2-Aug-13 20:15 
GeneralMy vote of 5 Pinmemberdyma27-Jul-13 7:10 
Questionabout position of the control's parent Pinmembergangireddyh@gmail.com18-Mar-13 0:17 
AnswerRe: about position of the control's parent PinmemberS.A.Esmaeily7-Apr-13 22:07 
GeneralRe: about position of the control's parent Pinmembergangireddyh@gmail.com14-Apr-13 22:00 
Questionsome roles are not clear .. PinmemberRemix Mixdox2-Feb-13 0:13 
AnswerRe: some roles are not clear .. PinmemberS.A.Esmaeily17-Mar-13 2:07 
Questionhi Pinmemberlagartus28-Jan-13 6:24 
GeneralMy vote of 5 Pinmembergeekbond17-Aug-12 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 27 Jul 2013
Article Copyright 2012 by S.A.Esmaeily
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid