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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:49:58+00:00 2026-05-23T08:49:58+00:00

Everyone, I have a WPF app that has a canvas that I have wrapped

  • 0

Everyone,

I have a WPF app that has a canvas that I have wrapped in a scroll Viewer. I have a slider in the status bar that allows the user to zoom in and out (just like Win 7’s mspaint).

Here is some of the XAML:

<ScrollViewer Name="Map"
              VerticalScrollBarVisibility="Auto"
              HorizontalScrollBarVisibility="Auto">
    <Canvas x:Name="WallsCanvas" Height="800" Width="1000" ClipToBounds="True">
        <Canvas.LayoutTransform>
            <ScaleTransform x:Name="WallsCanvasScale"
                            ScaleX="1" ScaleY="1" />
        </Canvas.LayoutTransform>
    </Canvas>
</ScrollViewer>

When I zoom in, and the scrollbars are visible, the scrollbars, no matter where they are set, jump to the middle.

It is exactly as if the value of the scrollbars stay the same but the max value increases.

What can I do to get them to … say, if they were in the lower right corner, to stay in the lower right corner after a zoom in or out?

BTW, here is my Zoom in and out code:

private void SliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    var scales = new []{.125, .25, .5, 1, 2, 4, 8};
    var scale = scales[(int)((Slider) sender).Value];

    ScaleChanged(scale, WallsCanvasScale);
}

private static void ScaleChanged(double scale, ScaleTransform st)
{
    st.ScaleX = scale;
    st.ScaleY = scale;
}

So, no rocket science in my code but …

Update idea: If I had access to the value and the max value of the scrollbars, could I get the percentage between the two and then after the zooming (scaling) I could re-apply the value of the scrollbar as a percentage of the max value????? But where is the value and the max value available?

Any help would be appreciated. I cannot think that I am the only one that has this problem since MSPaint (the Windows 7 version) works correctly and I assume it is a XAML app.

Here is a link (http://www.leesaunders.net/examples/zoomexample/zoomexample.zip) to a minimum working example project (VS 2010). When you run it, just move the scroll bars then zoom in a level, you will see the issue right away.

  • 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-23T08:49:59+00:00Added an answer on May 23, 2026 at 8:49 am

    You just need to offset the shift which results from the scale because it scales from (0,0). It’s a bit complicated but here’s a sketch of what the method in your sample could look like:

    private void SliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        var slider = (Slider)sender;
        if (!slider.IsLoaded)
        {
            slider.Loaded += (s, le) => SliderValueChanged(sender, e);
            return;
        }
    
        var scales = new[] { .125, .25, .5, 1, 2, 4, 8 };
        var scale = scales[(int)((Slider)sender).Value];
    
        // The "+20" are there to account for the scrollbars... i think. Not perfectly accurate.
        var relativeMiddle = new Point((Map.ActualWidth + 20) / 2, (Map.ActualHeight + 20) / 2);
        var oldLocation = CanvasScale.Transform(TemplateCanvas.PointFromScreen(relativeMiddle));
    
        ScaleChanged(scale, CanvasScale);
    
        var newLocation = CanvasScale.Transform(TemplateCanvas.PointFromScreen(relativeMiddle));
    
        var shift = newLocation - oldLocation;
    
        Map.ScrollToVerticalOffset(Map.VerticalOffset + shift.Y);
        Map.ScrollToHorizontalOffset(Map.HorizontalOffset + shift.X);
    
        lblScale.Content = scale.ToString("P1").Replace(".0", string.Empty);
    }
    

    Should be quite self-explanatory; the location of the center is measured before and after the scaling to calculate the shift of that point, which then is added to the current scroll-position.

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

Sidebar

Related Questions

This is an interesting conundrum. We have a WPF app that has a Vista-like
I have to write an app that is available to everyone, that is, no
Hey everyone I have a basic search function here that allows me to search
I have a central git repository that everyone pushes to for testing and integration,
I have a project which contains different components that everyone works on. We have
Hi everyone my problem today is I have a couple of buttons that slide
Hie Everyone I have 1st time developed SSRS in my WPF application.. but there
I have a WPF ListView that contains CheckBox column and other columns of the
Hello everyone I have converted a project in C# to F# that paints the
everyone I have some question about tasks in Linux, I know that all tasks

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.