How to get a WPF Viewbox coefficient of scaling applied

十年热恋 提交于 2019-11-29 04:19:24
Fredrik Hedblad

See this question: Get the size (after it has been "streched") of an item in a ViewBox

Basically, if you have a Viewbox called viewbox, you can get the ScaleTransform like this

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

You could also make an extension method for Viewbox which you can call like this

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;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!