更改旋转对象的RenderTransformOrigin属性

时间:2011-02-27 11:01:10

标签: silverlight rotatetransform

当RenderTransformOrigin属性发生变化时,如何使旋转的对象保持静止?运行下面的代码后,您可以通过旋转对象(滑块执行此操作),更改旋转原点(按钮)并再次旋转它来注意到对象稍微移动。我想要实现的是能够将对象旋转到不同的点而不会在我尝试在它们之间切换时四处移动。

<UserControl x:Class="ObjrctRotation.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">

<Canvas x:Name="C" Background="White">
    <Path x:Name="thing" RenderTransformOrigin="0,0" Stretch="Fill" Stroke="Black" StrokeThickness="2" Height="93.153" HorizontalAlignment="Left" VerticalAlignment="Top" Width="106.357" UseLayoutRounding="False" Data="M48.723255,124.82815 C41.00029,80.999809 81.000046,65.000069 112.99985,89.000092 C144.99965,113.00011 168.9996,169.00061 136.99989,169.0002 C105.00018,168.99979 104.9999,145.00015 81.000053,145.00015 C57.000206,145.00015 56.44622,168.65649 48.723255,124.82815 z" Canvas.Left="69" Canvas.Top="24" />
    <Slider Value="0" Minimum="0" Maximum="360" Width="100" Margin="300,0,0,272" ValueChanged="Slider_ValueChanged" />
    <TextBlock x:Name="test" Width="100" Height="20" Canvas.Left="300" Canvas.Top="24" />
    <Button Canvas.Left="300" Canvas.Top="24" Content="ChangeCenter" Height="20" Name="button1" Width="100" Click="button1_Click"/>
</Canvas>

 public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
    }
    private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        RotateTransform rotate = new RotateTransform()
        {
            Angle=e.NewValue
        };         

        thing.RenderTransform = rotate;
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        thing.RenderTransformOrigin = new Point(thing.RenderTransformOrigin.X + 0.2, thing.RenderTransformOrigin.Y + 0.2);
    }
}

1 个答案:

答案 0 :(得分:0)

将RenderTransformOrigin =“0,0”设置为RenderTransformOrigin =“0.5,0.5”,以便变换原点位于您想要旋转的对象的中间。