从代码中旋转对象

时间:2015-05-21 13:04:11

标签: c# wpf xaml

我想用C#代码旋转XAML中制作的多边形,但是我仍然停留在Propertypath上。有人知道我应该使用什么吗?

这是我现在拥有的C#代码:

public void Rotate()
{
    Storyboard rotate = new Storyboard();

    DoubleAnimation myDoubleAnimation = new DoubleAnimation();
    myDoubleAnimation.From = 0;
    myDoubleAnimation.To = 360;
    myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5));

    Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath("(Polygon.RenderTransform).(RotateTransform.Angle)"));
    Storyboard.SetTargetName(myDoubleAnimation, Arrowhead.Name);
    rotate.Children.Add(myDoubleAnimation);
    rotate.Begin(Arrowhead);
}

这是我想在XAML中旋转的多边形(三角形):

                <Polygon Fill="#41b1ff"
                 Stroke="Gray"
                 StrokeThickness="2"
                 Points="80,60,100,40,100,40,120,60" 
                 Grid.ColumnSpan="3"
                 Grid.RowSpan="3"
                 Name="Arrowhead"
                     />

1 个答案:

答案 0 :(得分:1)

您的代码似乎运行正常。您需要添加您尝试修改的转换:

<Polygon
        Fill="#41b1ff"
        Stroke="Gray"
        StrokeThickness="2"
        Points="80,60,100,40,100,40,120,60"
        Name="Arrowhead">
    <Polygon.RenderTransform>
        <RotateTransform/>
    </Polygon.RenderTransform>
</Polygon>