简体   繁体   English

RenderTransformOrigin 不起作用。UWP

[英]The RenderTransformOrigin does not work.UWP

I have a problem that I don't understand how to solve it.我有一个问题,我不明白如何解决它。 I have a UWP application.我有一个 UWP 应用程序。 In the XAML there is a Grid that has RenderTransformOrigin.在 XAML 中有一个具有 RenderTransformOrigin 的网格。 But what value I would give it doesn't matter when I change the width of the grid the width is calculated from the center.但是当我改变网格的宽度时,我会给它什么值并不重要,宽度是从中心计算的。

This is the XAML这是 XAML

<Page
x:Class="TestApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid Background="White">

    <Grid Background="Red" x:Name="image" RenderTransformOrigin="0,0">
        <Grid.RenderTransform>
            <CompositeTransform />
        </Grid.RenderTransform>
        <Border x:Name="LeftScale" Width="50" Height="50" Background="Black" HorizontalAlignment="Left" VerticalAlignment="Center" ManipulationDelta="LeftScale_ManipulationDelta" ManipulationMode="TranslateX,TranslateY" />

    </Grid>

</Grid>

And the C#和 C#

public sealed partial class MainPage : Page
{
    public Point Origin { get; set; }
    public MainPage()
    {
        this.InitializeComponent();
        image.Width = 500;
        image.Height = 500;
    }

    private void LeftScale_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        Origin = new Point(1, 0.5);
        image.RenderTransformOrigin = Origin;
        image.Width -= e.Delta.Translation.X;
    }
}

Thanks for attention.谢谢关注。

RenderTransformOrigin is suitable for transformation. RenderTransformOrigin适用于变换。 This transformation needs to be completed by the CompositeTransform you define.这个转换需要你定义的CompositeTransform来完成。 It cannot be changed by changing the Width property.它不能通过更改Width属性来更改。

You can rewrite the code and try this:您可以重写代码并尝试以下操作:

Page.xaml页面.xaml

<StackPanel>
    <Grid Background="Red" x:Name="image" RenderTransformOrigin="0,0" HorizontalAlignment="Center">
        <Grid.RenderTransform>
            <CompositeTransform x:Name="imageCompositeTransform"/>
        </Grid.RenderTransform>
    </Grid>
    <Slider Maximum="500" 
            HorizontalAlignment="Center"
            Width="500" 
            Margin="0,30,0,0" 
            ValueChanged="Slider_ValueChanged"/>
</StackPanel>

Page.xaml.cs页面.xaml.cs

public Point Origin { get; set; }
public MainPage()
{
    this.InitializeComponent();
    image.Width = 500;
    image.Height = 500;
}

private void Slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
    Origin = new Point(1, 0.5);
    image.RenderTransformOrigin = Origin;
    var proportion = e.NewValue / image.Width;
    imageCompositeTransform.ScaleX = 1 - proportion;
}

Transform the width of the control through ScaleX (not Width ) so that RenderTransformOrigin can take effect.通过ScaleX (不是Width )变换控件的WidthRenderTransformOrigin才能生效。


Update更新

If you wish to do the width transformation of the Grid through your original code, here is the method:如果你想通过你的原始代码来做 Grid 的宽度转换,这里是方法:

private double moveX = 0;

private void LeftScale_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
    moveX -= e.Delta.Translation.X;
    var proportion = moveX / image.Width;
    imageCompositeTransform.ScaleX = 1 - proportion;
}

However, because your Rectangle is inside the Grid , the width of the Rectangle will also change when the Grid width is transformed.但是,因为您的Rectangle位于Grid内部,所以当Grid宽度转换时, Rectangle的宽度也会发生变化。 If you want to keep the size of the Rectangle unchanged, it is recommended to move it out.如果要保持Rectangle的大小不变,建议将其移出。

Best regards.此致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM