Click here to Skip to main content
15,608,989 members
Articles / Desktop Programming / WPF
Posted 16 Aug 2017

Tagged as


8 bookmarked

Simple Behavior to Allow Zooming and Dragging a Contained Control

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
16 Aug 2017CPOL1 min read
This is a simple behavior that can be attached to a ContentControl and allows zooming and dragging the Content.


I inherited a solution that had some code behind to allow zooming the contents of a Window because the WPF design was for a 2560 x1440 display and was hard to see on a smaller display. I moved the code into a behavior and added the ability to drag the zoomed control. I also designed it so that the container did not have to be just a Window but could be any ContentControl.

Using the Code

Below is an example of how to use this behavior:

<ContentControl HorizontalAlignment="Stretch"
    <Image Source="interesting-pictures-asperatus-clouds.jpg"
           Stretch="Fill" />

The behavior can only be used on a Control that derives from a ContentControl because it depends on the Content property. In the example, a ContentControl is used but it will work on a Window or a UserControl. Also, for everything to look right, the Content has to fill the ContentControl. This is why the Image Stretch is set to Fill. You can see what happens the Content size is different from the ContrentControl size.

Image 1

Window at start up.

Image 2

Initial after zooming in.

Image 3

After zooming in and dragging as far as possible down and right.

Image 4

After zooming out (notice the content is centered).

To resize the contents, hold down the keyboard Control key and the scroll button on the mouse. To drag, hold down the Control key and the Left mouse button and drag. When zoomed out, the Content is centered on the ContentControl.


This is really not a very sophisticated control. It does not include slider controls when the contents are zoomed out, and does not include a way to reset the zoom, or to allow the zoom limits to be changed.


  • 08/16/17: Initial version


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

Written By
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at

Comments and Discussions

QuestionLooks Good, But... Pin
Kevin Marois17-Aug-17 11:21
professionalKevin Marois17-Aug-17 11:21 

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

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