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 618117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:30:36+00:00 2026-05-13T18:30:36+00:00

I have a Canvas inside a ScrollViewer. I want to have the user to

  • 0

I have a Canvas inside a ScrollViewer. I want to have the user to be able to grab the canvas and move it around, with the thumbs on the scrollbars updating appropriately.
My initial implementation calculates the offset on each mouse move, and updates the scrollbars:

 // Calculate the new drag distance
 Point newOffsetPos = e.GetPosition(MapCanvas);
 System.Diagnostics.Debug.WriteLine("   newOffsetPos : " + newOffsetPos.X + " " + newOffsetPos.Y);

 double deltaX = newOffsetPos.X - _offsetPosition.X ;
 double deltaY = newOffsetPos.Y - _offsetPosition.Y ;

 System.Diagnostics.Debug.WriteLine("   delta X / Y : " + deltaX + " " + deltaY);
 System.Diagnostics.Debug.WriteLine("   sv offsets X / Y : " + _scrollViewer.HorizontalOffset + " " + _scrollViewer.VerticalOffset);

 _scrollViewer.ScrollToHorizontalOffset(_scrollViewer.HorizontalOffset - deltaX);
 _scrollViewer.ScrollToVerticalOffset(_scrollViewer.VerticalOffset - deltaY);

 _offsetPosition = newOffsetPos;

While this works, it is not very smooth.
Is there a better way to do this? If Transforms are used, will the scrollbars update automagically when the Canvas is moved?
Thanks for any tips…

  • 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-13T18:30:36+00:00Added an answer on May 13, 2026 at 6:30 pm

    Actually this sort of problem is really a matter of using the right pattern to track the mouse. I’ve seen this issue in variety of cases not just in Silverlight.

    The best pattern is to trap the original locations of both mouse and subject, then recalculate the new offset from the fixed original values. That way the mouse stays planted solid at a single point on the image being panned. Here is my simple Repro:-

    Start with a fresh Silverlight Application in visual studio. Modify MainPage.Xaml thus:-

    <UserControl x:Class="SilverlightApplication1.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid x:Name="LayoutRoot" Background="White">
            <ScrollViewer x:Name="Scroller" HorizontalScrollBarVisibility="Auto">
                <Image x:Name="Map" Source="test.jpg" Width="1600" Height="1200" />
            </ScrollViewer>
        </Grid>
    </UserControl>
    

    Add the following code to the MainPage.xaml.cs file:-

        public MainPage()
        {
            InitializeComponent();
            Map.MouseLeftButtonDown += new MouseButtonEventHandler(Map_MouseLeftButtonDown);
        }
    
        void Map_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point mapOrigin = new Point(Scroller.HorizontalOffset, Scroller.VerticalOffset);
            Point mouseOrigin = e.GetPosition(Application.Current.RootVisual);
    
            MouseEventHandler moveHandler = null;
            MouseButtonEventHandler upHandler = null;
    
            moveHandler = (s, args) =>
            {
                Point mouseNew = args.GetPosition(Application.Current.RootVisual);
                Scroller.ScrollToHorizontalOffset(mapOrigin.X - (mouseNew.X - mouseOrigin.X));
                Scroller.ScrollToVerticalOffset(mapOrigin.Y - (mouseNew.Y - mouseOrigin.Y));
            };
    
    
            upHandler = (s, args) =>
            {
                Scroller.MouseMove -= moveHandler;
                Scroller.MouseLeftButtonUp -= upHandler;
            };
    
            Scroller.MouseMove += moveHandler;
            Scroller.MouseLeftButtonUp += upHandler;
        }
    }
    

    Give it a reasonably large test.jpg (doesn’t need to be 1600×1200 Image will scale it).

    You’ll note that when dragging the mouse remains exactly over a fixed point in the image until you hit a boundary. Move the mouse as fast as you like it always tracks, this is because it doesn’t depend on deltas being accurate and up-to-date. The only variable is the current mouse position, the other values remain fixed as they were at mouse down.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Canvas inside a UserControl that can be panned around. I want
I have read that placing a canvas inside a scrollviewer won't work because the
I have a Shape inside Canvas , like this: <ScrollViewer> <Border Height=342 Width=470 HorizontalAlignment=Left
I have Paths inside my Canvas and I want to change the Z index
I have a canvas that contains several bitmap images, I want the user to
I have a Canvas which has many components inside it and those again, have
I have a Canvas in a Flex application which has items inside it that
I have a Grid inside a Canvas defined like this: <Canvas x:Name=outerCanvas> <Grid Grid.Row=1
I have a canvas on my page <canvas id=oneCanvas width=250px heigth=250px></canvas>​ then I want
I have a canvas in Flex that shall be able only to be scrolled

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.