【问题标题】:How set different origin centers for scaletransform and rotatetransform?如何为 scaletransform 和 rotatetransform 设置不同的原点?
【发布时间】:2016-07-11 14:47:33
【问题描述】:

如何为 scaletransform 和 rotatetransform 设置不同的原点?

示例 xaml:

<ScaleTransform ScaleX="{Binding Zoom}"
                                        ScaleY="{Binding Zoom}"
                                        CenterX="0"
                                        CenterY="0" />
<RotateTransform Angle="{Binding RotateAngle}"
                                         CenterX="0.5"
                                         CenterY="0.5"/>

所有想到的:当您单击缩放或旋转滑块时,通过绑定更改 RenderTransformOrigin。这是正确的方法吗?附言

对不起我的英语。

【问题讨论】:

  • 为什么不同时绑定CenterX和CenterY属性呢?
  • 我不知道如何解决我的问题。例如,当我在缩放后更改 RenderTransformOrigin 并使用不同的原点进行旋转时,我的上一个缩放比例会用新的缩放比例重新渲染。我想我需要顺序转换。

标签: c# wpf xaml transform


【解决方案1】:

我可能会通过使用TranslateTransforms 在每次操作之前移动对象来解决这个问题。您还可以使用变换的Inverse 将其无效(例如,在下一个操作之前将对象移回原点)。

【讨论】:

  • 我认为我应该在每次转换后使用 TranslateTransforms,这可能会解决我的问题。感谢正确的方向。
【解决方案2】:

在 H.B.回答我解决了我的问题。也许有人会对我的解决方案有用。对我来说像魅力一样工作:以 origin=0.5,0.5 旋转;比例为 0,0

XAML:

<ScrollViewer Grid.Row="1"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Auto">
            <Canvas x:Name="canvas"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Width="{Binding CanvasW}"
                    Height="{Binding CanvasH}"
                    >
        <Image HorizontalAlignment="Center"
                       Stretch="None"
                       x:Name="image1"
                       VerticalAlignment="Center"
                       RenderTransformOrigin="{Binding TransformOrigin}">
                    <Image.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="{Binding Zoom}"
                                            ScaleY="{Binding Zoom}" />
                            <RotateTransform Angle="{Binding RotateAngle}" />
                            <TranslateTransform X="{Binding TransX}"
                                                Y="{Binding TransY}" />
                        </TransformGroup>
                    </Image.RenderTransform>
        </Image>
        </Canvas>
<ScrollViewer/>

视图模型:

public double Zoom
        {
            get { return zoom; }
            set 
            {
                if (value != zoom)
                {
                    zoom = value;
                    RaisePropertyChanged("Zoom");
                    RaisePropertyChanged("CanvasH");
                    RaisePropertyChanged("CanvasW");
                    RaisePropertyChanged("TransX");
                    RaisePropertyChanged("TransY");
                }
            }
        }

public double TransX
        {
            get 
            {
                if (imageSource != null)
                {
                    return ((imageSource.Width *zoom - imageSource.Width)/2;
                }
                return 0;
            }
        }

public double TransY
        {
            get
            {
                if (imageSource != null)
                {
                    return (imageSource.Height * zoom - imageSource.Height) / 2;
                }
                return 0;
            }
        }
public double CanvasH
        {
            get 
            {
                if (imageSource!=null)
                {
                    return imageSource.Height*zoom;
                }
                return canvasH;
            }
}
public double CanvasW
        {
            get
            {
                if (imageSource != null)
                {
                    return imageSource.Width*zoom;
                }
                return canvasW;
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    相关资源
    最近更新 更多