Click here to Skip to main content
11,720,557 members (69,430 online)
Click here to Skip to main content

Drag and Drop WPF Controls

, 27 Jul 2013 CPOL 41.8K 7.2K 19
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.

You may also be interested in...

Comments and Discussions

 
BugMouse slipping off some Contents Pin
Member 1060332617-Feb-14 7:09
memberMember 1060332617-Feb-14 7:09 
GeneralRe: Mouse slipping off some Contents Pin
S.A.Esmaeily17-Feb-14 19:27
professionalS.A.Esmaeily17-Feb-14 19:27 
QuestionUsing Point Pin
heulendoch18-Nov-13 2:01
memberheulendoch18-Nov-13 2:01 
SuggestionNice approach Pin
BrunoH7921-Oct-13 23:14
memberBrunoH7921-Oct-13 23:14 
GeneralRe: Nice approach Pin
S.A.Esmaeily22-Oct-13 19:45
memberS.A.Esmaeily22-Oct-13 19:45 
SuggestionMy vote of 5 Pin
MehdiNaseri2-Aug-13 15:58
memberMehdiNaseri2-Aug-13 15:58 
GeneralRe: My vote of 5 Pin
S.A.Esmaeily2-Aug-13 19:15
memberS.A.Esmaeily2-Aug-13 19:15 
GeneralMy vote of 5 Pin
dyma27-Jul-13 6:10
memberdyma27-Jul-13 6:10 
Questionabout position of the control's parent Pin
gangireddyh@gmail.com17-Mar-13 23:17
membergangireddyh@gmail.com17-Mar-13 23:17 
AnswerRe: about position of the control's parent Pin
S.A.Esmaeily7-Apr-13 21:07
memberS.A.Esmaeily7-Apr-13 21:07 
GeneralRe: about position of the control's parent Pin
gangireddyh@gmail.com14-Apr-13 21:00
membergangireddyh@gmail.com14-Apr-13 21:00 
Questionsome roles are not clear .. Pin
Remix Mixdox1-Feb-13 23:13
memberRemix Mixdox1-Feb-13 23:13 
AnswerRe: some roles are not clear .. Pin
S.A.Esmaeily17-Mar-13 1:07
memberS.A.Esmaeily17-Mar-13 1:07 
Questionhi Pin
lagartus28-Jan-13 5:24
memberlagartus28-Jan-13 5:24 
GeneralMy vote of 5 Pin
geekbond17-Aug-12 5:28
membergeekbond17-Aug-12 5: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
Web03 | 2.8.150901.1 | Last Updated 27 Jul 2013
Article Copyright 2012 by S.A.Esmaeily
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid