Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Behavior to Allow Zooming and Dragging a Contained Control

0.00/5 (No votes)
16 Aug 2017 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here