How to get applicable WPF Viewbox scaling factor

If we use a WPF (Silverlight) Viewbox with Stretch="UniformToFill" or Stretch="Uniform" when it retains the original aspect ratio of the content , how can we find out the current zoom ratio that was applied to the content?

Note. We do not always know the exact initial dimensions of the content (for example, this is a grid with a lot of materials in it).

+8
layout wpf silverlight viewbox
source share
1 answer

See this question: Get the size (after it has been "stretched") of the element in the ViewBox

Basically, if you have a Viewbox named viewbox, you can get a ScaleTransform as follows

 ContainerVisual child = VisualTreeHelper.GetChild(viewbox, 0) as ContainerVisual; ScaleTransform scale = child.Transform as ScaleTransform; 

You can also create an extension method for the Viewbox , which you can name as follows

 viewbox.GetScaleFactor(); 

ViewBoxExtensions

 public static class ViewBoxExtensions { public static double GetScaleFactor(this Viewbox viewbox) { if (viewbox.Child == null || (viewbox.Child is FrameworkElement) == false) { return double.NaN; } FrameworkElement child = viewbox.Child as FrameworkElement; return viewbox.ActualWidth / child.ActualWidth; } } 
+14
source share

All Articles