How to get a WPF Viewbox coefficient of scaling applied

前端 未结 1 433
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 22:50

In case we use WPF (Silverlight) Viewbox with Stretch=\"UniformToFill\" or Stretch=\"Uniform\" when it preserves content\'s native asp

相关标签:
1条回答
  • 2020-12-16 23:30

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题