Click here to Skip to main content
Click here to Skip to main content

WPF simple zoom and drag support in a ScrollViewer

, 6 Nov 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
A sample describing a way to zoom with the mouse wheel or a slider and drag limited content which is hosted by a ScrollViewer.



I've been looking for a while for Open Source solutions that show a simple way of zooming and dragging arbitrary content which is hosted and managed by a ScrollViewer.

As I did not find a free one, I decided to write my own one and share it with you. It supports zooming by a slider as well as by the mouse wheel.

Using the code

If you have any questions regarding the usage, please feel free to post and I'll try to get back to you ASAP. 

The main view is defined by the XAML below. The content that is to be zoomed and dragged is part of the "grid" control.

<Window x:Class="ZoomExample.MainWindow"
        Title="MainWindow" Height="500" Width="500">
                <ResourceDictionary Source="Resources.xaml"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        <Slider Grid.Column="0" Orientation="Vertical" 
           HorizontalAlignment="Left" Minimum="1" x:Name="slider"/>
        <ScrollViewer Name="scrollViewer" Grid.Column="1" 
            <Grid Name="grid" Width="400" 
              Height="400" RenderTransformOrigin="0.5,0.5">
                        <ScaleTransform x:Name="scaleTransform"/>
                <Viewbox Grid.Column="0" Grid.Row="0">
                    <ContentPresenter Content="{StaticResource Kompass}"/>

Zooming and dragging is managed by the code-behind:

public partial class MainWindow : Window
    Point? lastCenterPositionOnTarget;
    Point? lastMousePositionOnTarget;
    Point? lastDragPoint;

    public MainWindow()

        scrollViewer.ScrollChanged += OnScrollViewerScrollChanged;
        scrollViewer.MouseLeftButtonUp += OnMouseLeftButtonUp;
        scrollViewer.PreviewMouseLeftButtonUp += OnMouseLeftButtonUp;
        scrollViewer.PreviewMouseWheel += OnPreviewMouseWheel;

        scrollViewer.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown;
        scrollViewer.MouseMove += OnMouseMove;

        slider.ValueChanged += OnSliderValueChanged;

    void OnMouseMove(object sender, MouseEventArgs e)
        if (lastDragPoint.HasValue)
            Point posNow = e.GetPosition(scrollViewer);

            double dX = posNow.X - lastDragPoint.Value.X;
            double dY = posNow.Y - lastDragPoint.Value.Y;

            lastDragPoint = posNow;

            scrollViewer.ScrollToHorizontalOffset(scrollViewer.HorizontalOffset - dX);
            scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - dY);

    void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        var mousePos = e.GetPosition(scrollViewer);
        if (mousePos.X <= scrollViewer.ViewportWidth && mousePos.Y < 
            scrollViewer.ViewportHeight) //make sure we still can use the scrollbars
            scrollViewer.Cursor = Cursors.SizeAll;
            lastDragPoint = mousePos;

    void OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
        lastMousePositionOnTarget = Mouse.GetPosition(grid);

        if (e.Delta > 0)
            slider.Value += 1;
        if (e.Delta < 0)
            slider.Value -= 1;

        e.Handled = true;

    void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        scrollViewer.Cursor = Cursors.Arrow;
        lastDragPoint = null;

    void OnSliderValueChanged(object sender, 
         RoutedPropertyChangedEventArgs<double> e)
        scaleTransform.ScaleX = e.NewValue;
        scaleTransform.ScaleY = e.NewValue;

        var centerOfViewport = new Point(scrollViewer.ViewportWidth/2, 
        lastCenterPositionOnTarget = scrollViewer.TranslatePoint(centerOfViewport, grid);

    void OnScrollViewerScrollChanged(object sender, ScrollChangedEventArgs e)
        if (e.ExtentHeightChange != 0 || e.ExtentWidthChange != 0)
            Point? targetBefore = null;
            Point? targetNow = null;

            if (!lastMousePositionOnTarget.HasValue)
                if (lastCenterPositionOnTarget.HasValue)
                    var centerOfViewport = new Point(scrollViewer.ViewportWidth/2, 
                    Point centerOfTargetNow = 
                          scrollViewer.TranslatePoint(centerOfViewport, grid);

                    targetBefore = lastCenterPositionOnTarget;
                    targetNow = centerOfTargetNow;
                targetBefore = lastMousePositionOnTarget;
                targetNow = Mouse.GetPosition(grid);

                lastMousePositionOnTarget = null;

            if (targetBefore.HasValue)
                double dXInTargetPixels = targetNow.Value.X - targetBefore.Value.X;
                double dYInTargetPixels = targetNow.Value.Y - targetBefore.Value.Y;

                double multiplicatorX = e.ExtentWidth/grid.Width;
                double multiplicatorY = e.ExtentHeight/grid.Height;

                double newOffsetX = scrollViewer.HorizontalOffset - 
                double newOffsetY = scrollViewer.VerticalOffset - 

                if (double.IsNaN(newOffsetX) || double.IsNaN(newOffsetY))



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kevin Stumpf

Germany Germany
No Biography provided

Comments and Discussions

QuestionZoom on a click PinmemberShyamala_12316-Feb-15 19:52 
GeneralMy vote of 5 Pinmemberamir_pro3-Feb-15 9:45 
GeneralMy vote of 5 Pinmemberamir_pro3-Feb-15 9:43 
QuestionGreat work PinmemberGPans11-Oct-13 10:53 
GeneralMy vote of 5 PinmemberBijay Kant Salotry19-Sep-13 7:52 
GeneralMy vote of 5 PingroupJohn Bracey20-Jun-13 22:50 
NewsMy vote of 5 PinmemberShahin Khorshidnia11-Jun-13 22:19 
GeneralMy vote of 5 PinmemberAdolfoPerez6-Jun-13 5:27 
Questionrotation Pinmemberredjzuzzj4-Jun-13 16:22 
QuestionSplendid! PinmemberPorgram Lover24-Feb-13 12:34 
GeneralMy vote of 5 Pinmemberarturo134631-Oct-12 6:37 
GeneralMy vote of 5 PinmemberMember 406769615-Jun-12 0:21 
GeneralMy vote of 5 Pinmembernjdnjdnjdnjdnjd16-May-12 7:27 
GeneralMy vote of 5 PinmemberDezfoul18-Apr-12 20:29 
QuestionCreated some helper classes [modified] Pinmemberkgoulding18-Jan-12 11:03 
QuestionRe: Created some helper classes PinmemberMember 94426811-Jul-13 23:24 
GeneralMy vote of 5 PinmemberUbloobok22-Oct-11 22:46 
GeneralMy vote of 5 PinmemberKoss8712-Jun-11 15:22 
GeneralThanks for the Demo! Pinmember(noor)31-Mar-11 4:49 
GeneralSounds familiar... PinmemberAshley Davis30-Jul-10 4:03 
GeneralLike it, been there too, I did a Friction one too PinmvpSacha Barber30-Jul-10 0:11 
GeneralProblem to download Pinmembersinun29-Jul-10 12:46 
GeneralRe: Problem to download PinmemberD-Lay29-Jul-10 13:25 
GeneralFixed PinmemberKevin Stumpf29-Jul-10 15:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 7 Nov 2013
Article Copyright 2010 by Kevin Stumpf
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid