Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 380751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:01:09+00:00 2026-05-12T15:01:09+00:00

I have a Canvas in which user can add UserControl subclasses containing a form.

  • 0

I have a Canvas in which user can add UserControl subclasses containing a form. User should be able to drag these UserControl around the Canvas.

What’s the best practice to do this with WPF?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:01:09+00:00Added an answer on May 12, 2026 at 3:01 pm

    This is done in silverlight and not in WPF, but it should work the same.

    Create two private properties on the control:

    protected bool isDragging;  
    private Point clickPosition;
    

    Then attatch some event handlers in the constructor of the control:

    this.MouseLeftButtonDown += new MouseButtonEventHandler(Control_MouseLeftButtonDown);
    this.MouseLeftButtonUp += new MouseButtonEventHandler(Control_MouseLeftButtonUp);
    this.MouseMove += new MouseEventHandler(Control_MouseMove);
    

    Now create those methods:

    private void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        isDragging = true;
        var draggableControl = sender as UserControl;
        clickPosition = e.GetPosition(this);
        draggableControl.CaptureMouse();
    }
    
    private void Control_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        isDragging = false;
        var draggable = sender as UserControl;
        draggable.ReleaseMouseCapture();
    }
    
    private void Control_MouseMove(object sender, MouseEventArgs e)
    {
        var draggableControl = sender as UserControl;
    
        if (isDragging && draggableControl != null)
        {
            Point currentPosition = e.GetPosition(this.Parent as UIElement);
    
            var transform = draggableControl.RenderTransform as TranslateTransform;
            if (transform == null)
            {
                transform = new TranslateTransform();
                draggableControl.RenderTransform = transform;
            }
    
            transform.X = currentPosition.X - clickPosition.X;
            transform.Y = currentPosition.Y - clickPosition.Y;
        }
    }
    

    A few things to note here:
    1. This does not have to be in a canvas. It can be in a stackpanel, or grid as well.
    2. This makes the entire control draggable, that means if you click anywhere in the control and drag it will drag the whole control. Not sure if thats exactly what you want.

    Edit-
    Expanding on some of the specifics in your question:
    The best way that I would implement this is to create a class that inherits from UserControl, maybe called DraggableControl that is built with this code, then all draggable controls should extend the DraggableControl.

    Edit 2 – There is small issue when you have a datagrid in this control. If you sort a column in the datagrid the MouseLeftButtonUp event never fires. I have updated the code so that isDragging is protected. I found the best solution is to tie this anonymous method to the LostMouseCapture event of the datagrid:

    this.MyDataGrid.LostMouseCapture += (sender, e) => { this.isDragging = false; };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 199k
  • Answers 199k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just a couple of ideas I've got in my head… May 12, 2026 at 7:45 pm
  • Editorial Team
    Editorial Team added an answer If you want a method in one class to know… May 12, 2026 at 7:45 pm
  • Editorial Team
    Editorial Team added an answer In your CilkProcInfo structure, inlet is a pointer to a… May 12, 2026 at 7:44 pm

Related Questions

I have had major problems with getting UserControls to work in XAML - I
I have recently begin creating an image editing tool which will cater to a
I was wondering if it is possible to include SVG content inside a panel
i am using java. i want to draw rectangle based on mousedrag event. if
I'm building an application on top of canvas, it consists of a simple DOM

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.