Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zooming into a Canvas without modifying the size of specific child controls

Tags:

wpf

zooming

So I realize that I am venturing outside of the intended use of a Canvas here and will likely have to come up with a more manual solution. However, not being overly experienced in WPF I was hoping that there may be some solution which would allow me to continue using a Canvas control and the features it gives you for free.

The issue revolves around a Canvas which is used to zoom in and out of an image and some number of child controls that belong to the Canvas. These child controls are to be placed at various positions on the image and, as such, the Canvas works nicely in that it handles all of the layout/positioning for me as I zoom in or out.

However, one drawback is that the Canvas scales these child controls up as I zoom into the image, causing them to become too large to be usable in practice. What I am looking for is a solution that allows me to zoom into an image that belongs to a canvas without also zooming up the size of the child controls, preferably handling the layout for me.

I have tried modifying the width and height of these child controls as the zoom factor increases or decreases, but there is a slight lag time and it all looks a bit 'jerky'.

If it comes down to it I will simply do all of the zooming/panning/layout myself, but I thought I would ask first just to make sure that I am not missing anything that would allow me to tell the Canvas to not scale the size of certain controls. Thanks in advance.

like image 212
Ed S. Avatar asked May 15 '11 18:05

Ed S.


2 Answers

You can bind the children's RenderTransform to the inverse of the Canvas' transform, see my answer to this similar question on rotation.

like image 192
H.B. Avatar answered Dec 21 '22 23:12

H.B.


This is more of a thought than an answer, but what if you set a transform on the element that you did not want scaled that was the opposite of the canvas itself. So for example, if the canvas had a scale transform of 2.0, set the element to have a scale transform of 0.5. You could probably accomplish this by binding the transform values together using a value converter.

You'll probably want to make sure the element has a render transform origin of 0.5,0.5 so that it scales from the center.

like image 41
Josh Avatar answered Dec 21 '22 23:12

Josh