Click here to Skip to main content
13,054,779 members (73,278 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 17 Aug 2012

Drag and Drop WPF Controls

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


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);


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


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


About the Author

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

QuestionControl Inside the Canvas Pin
asooom02012-Oct-15 2:23
memberasooom02012-Oct-15 2:23 
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 
Salam , i cant understand the role of :
Path1 = new Line();
            Path1.Width = DesigningCanvas.Width;
            Path1.Height = DesigningCanvas.Height;
            Path1.Stroke = Brushes.DarkGray;
            Path1.StrokeDashArray = new DoubleCollection(Dots);
            Path2 = new Line();
            Path2.Width = DesigningCanvas.Width;
            Path2.Height = DesigningCanvas.Height;
            Path2.Stroke = Brushes.DarkGray;
            Path2.StrokeDashArray = new DoubleCollection(Dots);

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    Praise Praise    Rant Rant    Admin Admin   

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

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