Click here to Skip to main content
13,352,280 members (75,799 online)
Click here to Skip to main content
Add your own
alternative version

Stats

3.4K views
85 downloads
5 bookmarked
Posted 16 Aug 2017

Simple Behavior to Allow Zooming and Dragging a Contained Control

, 16 Aug 2017
Rate this:
Please Sign up or sign in to vote.
This is a simple behavior that can be attached to a ContentControl and allows zooming and dragging the Content.

Introduction

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"
                VerticalAlignment="Stretch"
                local:WindowZoomBehavior.Enable="True">
    <Image Source="interesting-pictures-asperatus-clouds.jpg"
           Stretch="Fill" />
</ContentControl>

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.

Window at start up.

Initial after zooming in.

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

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.

Conclusion

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.

History

  • 08/16/17: Initial version

License

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

Share

About the Author

Clifford Nelson
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 qck1@hotmail.com.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionLooks Good, But... Pin
Kevin Marois17-Aug-17 12:21
professionalKevin Marois17-Aug-17 12: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 16 Aug 2017
Article Copyright 2017 by Clifford Nelson
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid