c# - 从代码旋转对象

标签 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"
                     />

最佳答案

您的代码似乎工作正常。您需要添加您要修改的转换:

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

关于c# - 从代码旋转对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30374627/

相关文章:

c# - GetManifestResourceStream - 它如何帮助缓存?

c# - 如何将数据从主窗口传递到主窗口内的用户控件?

c# - 如何将数据从打开的窗口发送回 WPF 中的打开器

c# - ListBox Item 将冲突的属性返回给子项

c# - 在 Windows 应用商店应用程序中动态填充组合框

C# : concatenate 2 MP3 files

c# - mvc4复选框更新数据库

c# - 服务定位协议(protocol)的示例客户端实现?

c# - 跟踪用于将 native C++ DLL 包装到 .NET 的 DLL 依赖项

c# - WPF 保留 Tab 控件状态