Search code examples
xamlwindows-8windows-runtimeviewboxwinrt-xaml

Calculate ViewBox ScaleFactor on a Metro Style App


Is there any way to get the scale factor by which a ViewBox scales its content in a XAML Windows 8 Metro Style App


Solution

  • The WinRTXAMLToolit has an extension for this.

    public static double GetChildScaleX(this Viewbox viewbox)
    {
        if (viewbox.Child == null)
            throw new InvalidOperationException("Can't tell effective scale of a Viewbox child for a Viewbox with no child.");
    
        var fe = viewbox.Child as FrameworkElement;
    
        if (fe == null)
            throw new InvalidOperationException("Can't tell effective scale of a Viewbox child for a Viewbox with a child that is not a FrameworkElement.");
    
        if (fe.ActualWidth == 0)
            throw new InvalidOperationException("Can't tell effective scale of a Viewbox child for a Viewbox with a child that is not laid out.");
    
        return viewbox.ActualWidth / fe.ActualWidth;
    }
    

    GetChildScaleY is the same, but with Heights.