Click here to Skip to main content
15,445,757 members
Articles / Desktop Programming / WPF
Tip/Trick
Posted 17 Aug 2012

Tagged as

Stats

93.3K views
12.8K downloads
25 bookmarked

Drag and Drop WPF Controls

Rate me:
Please Sign up or sign in to vote.
4.88/5 (24 votes)
27 Jul 2013CPOL1 min read
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:

C#
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:

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

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

C#
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)


Written By
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

 
GeneralMy vote of 5 Pin
Member 1332584614-Mar-22 8:16
MemberMember 1332584614-Mar-22 8:16 
QuestionMy Windows Form Chart does not Drag at WPF UserControl or Window Pin
novreis1-Feb-21 9:23
Membernovreis1-Feb-21 9:23 
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
Amin Esmaeily17-Feb-14 19:27
professionalAmin 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
Amin Esmaeily22-Oct-13 19:45
professionalAmin Esmaeily22-Oct-13 19:45 
SuggestionMy vote of 5 Pin
MehdiNaseri2-Aug-13 15:58
professionalMehdiNaseri2-Aug-13 15:58 
GeneralRe: My vote of 5 Pin
Amin Esmaeily2-Aug-13 19:15
professionalAmin 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
G.NaaNee17-Mar-13 23:17
MemberG.NaaNee17-Mar-13 23:17 
AnswerRe: about position of the control's parent Pin
Amin Esmaeily7-Apr-13 21:07
professionalAmin Esmaeily7-Apr-13 21:07 
GeneralRe: about position of the control's parent Pin
G.NaaNee14-Apr-13 21:00
MemberG.NaaNee14-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
Amin Esmaeily17-Mar-13 1:07
professionalAmin Esmaeily17-Mar-13 1:07 
Questionhi Pin
lagartus28-Jan-13 5:24
Memberlagartus28-Jan-13 5:24 
GeneralMy vote of 5 Pin
Akinmade Bond17-Aug-12 5:28
professionalAkinmade Bond17-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.