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
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.