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

  • Home
  • SEARCH
  • 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 1016889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:37:01+00:00 2026-05-16T10:37:01+00:00

On my quest to implement a very simple ‘drag’ mechanism to my application (which

  • 0

On my quest to implement a very simple ‘drag’ mechanism to my application (which consists of multiple canvasses nested inside a ‘parent’ canvas) I’ve come up with the following bits of code:

(Showing only the relevant bits to save space)

MainWindow.xaml

    <Canvas Name="parentCanvas" Background="#FFE8CACA">
        <Canvas Name="myCanvas" Height="100" Width="200" Background="#FFB4FFB4">
            <Thumb Name="myThumb" Canvas.Left="0" Canvas.Top="0" Background="Blue" Width="200" Height="20" DragDelta="onDragDelta" />
        </Canvas>

        <!-- debug -->
        <Button Content="Zero" Height="51" Name="button1" Width="46" Click="button1_Click" Canvas.Left="14" Canvas.Top="302" />
        <Label Name="pos" Width="499" Canvas.Left="77" Canvas.Top="302" Height="26" />
        <Label Name="changes" Height="28" Canvas.Left="77" Canvas.Top="325" Width="499" />
    </Canvas>

MainWindow.xaml.cs

    void onDragDelta(object sender, DragDeltaEventArgs e)
    {
        Canvas.SetLeft(myCanvas, e.HorizontalChange);
        Canvas.SetTop(myCanvas, e.VerticalChange);

        //debug info
        pos.Content = "Left: " + Canvas.GetLeft(myCanvas) + ", Top: " + Canvas.GetTop(myCanvas);
        changes.Content = "Horizontal: " + e.HorizontalChange + ", Vertical: " + e.VerticalChange;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        Canvas.SetLeft(myCanvas, 0);
        Canvas.SetTop(myCanvas, 0);
    }

Which seems to work, randomly! It seems to follow the mouse movements but I am having trouble interpreting the DragDeltaEventArgs, which causes a very unstable movement of the canvas. It’s kind of hard to explain so here is a short video I’ve captured of it: http://img84.imageshack.us/img84/6614/drag.mp4

Any comments/suggestions will be much appreciated as I’ve been staring at this for a while and can’t figure out what to do with it 🙁

  • 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-16T10:37:01+00:00Added an answer on May 16, 2026 at 10:37 am

    The Horizontal and Vertical changes are the amount moved since the previous event so you need to add them to the current position.

    Canvas.SetLeft(myCanvas, Canvas.GetLeft(myCanvas) + e.HorizontalChange);
    Canvas.SetTop(myCanvas, Canvas.GetTop(myCanvas) + e.VerticalChange);
    

    You also need to set a starting position for the canvas otherwise you get NaN.

    <Canvas Name="myCanvas" Height="100" Width="200" Background="#FFB4FFB4" Canvas.Left="0" Canvas.Top="0">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.