WPF中的ScaleTransform呈现的大小错误



我试图理解ScaleTransform(在Windows Phone 8.0下测试)为什么使用控件的父维度而不是控件本身进行渲染。我已经建立了一个演示应用程序来显示这种行为,如下所示。

维度和层次结构是有原因的,这就是为什么我手动设置WidthHeight,因为它非常接近真实的应用程序。

我所期望的是child1(黄色的),它是768x1228,具有0.625的比例,会将自己渲染为480x768,但实际情况是,它渲染为300x480(例如,它在4800.625%而不是768处渲染)。

有线索吗?

public partial class MainPage : PhoneApplicationPage
{
    private Grid root;
    public MainPage()
    {
        InitializeComponent();
        root = new Grid() {
            Width = 480,
            Height = 768,
            HorizontalAlignment = HorizontalAlignment.Left,
            VerticalAlignment = VerticalAlignment.Top,
            Background = new SolidColorBrush(Colors.Blue)
        };
        Content = root;
        Loaded += MainPage_Loaded;
    }
    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        var parent1 = new Grid {
            Background = new SolidColorBrush(Colors.Red),
            HorizontalAlignment = HorizontalAlignment.Left,
            VerticalAlignment = VerticalAlignment.Top,
            Width = 480,
            Height = 768
        };
        root.Children.Add(parent1);
        var child1 = new Grid {
            Background = new SolidColorBrush(Colors.Yellow),
            Width = 768,
            HorizontalAlignment = HorizontalAlignment.Left,
            VerticalAlignment = VerticalAlignment.Top,
            Height = 1228,
            RenderTransform = new ScaleTransform() {
                CenterX = 0,
                CenterY = 0,
                ScaleX = 0.625,
                ScaleY = 0.625
            }
        };
        parent1.Children.Add(child1);
    }
}    

好吧,看起来这一切都是由于Grid组件剪裁了它的子元素,如中所述http://wpf.2000things.com/tag/clipping.在我更改代码以使Canvas搁浅在我的Grid之后,该应用程序按预期工作。

不过,我仍然觉得这个"解决方案"很奇怪。

相关内容

最新更新