如何获得WPF Viewbox缩放比例系数 [英] How to get a WPF Viewbox coefficient of scaling applied

查看:1281
本文介绍了如何获得WPF Viewbox缩放比例系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们在保留内容的原始长宽比时将WPF(Silverlight)ViewboxStretch="UniformToFill"Stretch="Uniform"一起使用,我们如何才能知道应用于内容的当前缩放系数?

In case we use WPF (Silverlight) Viewbox with Stretch="UniformToFill" or Stretch="Uniform" when it preserves content's native aspect ratio, how could we get knowing the current coefficient of scaling which were applied to the content?

注意:我们并不总是知道内容的确切初始尺寸(例如,它是一个包含很多内容的网格).

Note: we not always know the exact initial dimensions of the content (for example it's a Grid with lots of stuff in it).

推荐答案

看到此问题:

基本上,如果您有一个名为viewbox的Viewbox,则可以像这样获得ScaleTransform

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;

您还可以为Viewbox创建扩展方法,您可以这样调用

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;
    }
}

这篇关于如何获得WPF Viewbox缩放比例系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆