开发者

Rotate a WPF Canvas derived component using code behind

开发者 https://www.devze.com 2023-03-11 08:17 出处:网络
I\'m wanting to make a component that inherits from rotate canvas using a storyboard. But when I try nothing happens.

I'm wanting to make a component that inherits from rotate canvas using a storyboard. But when I try nothing happens.

Eventually I also want to dynamically change the speed of rotation to a stop. But first, i need rotate the componente.

This is the code for the component:

class MyToy : Canvas
{
    public MyToy()
    {
        this.Background = System.Windows.Media.Brushes.Green;
        this.Width = 300;
        this.Height = 300;

        Polyline poly = new Polyline();
        poly.Points.Add(new Point(25, 25));
        poly.Points.Add(new Point(0, 50));
        poly.Points.Add(new Point(25, 75));
        poly.Points.Add(new Point(50, 50));
        poly.Points.Add(new Point(25, 25));
        poly.Points.Add(new Point(25, 0));
        poly.Stroke = System.Windows.Media.Brushes.Blue;
        poly.StrokeThickness = 10;

        this.Children.Add(poly);
        Canvas.SetLeft(poly, 120);
        Canvas.SetTop(poly, 120);
    }
}

the window xaml code is:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="500" Width="500">
    <Canvas x:Name="myCanvas">
        <Button Canvas.Left="0" Canvas.Top="0" Content="Rotate" Height="23" N开发者_StackOverflow中文版ame="button1" Width="111" Click="button1_Click" />
    </Canvas>
</Window>

And finaly, the code behind, where i create the storyboard is:

public partial class Window1 : Window
{
    MyToy myToy;
    RotateTransform transform;

    public Window1()
    {
        InitializeComponent();

        myToy = new MyToy();
        transform = new RotateTransform();   
        // transform.Name = "MyToy1Transform";
        myToy.RenderTransform = transform;
        // this.RegisterName(transform.Name, transform);

        myToy.Name = "MyToy1";
        this.RegisterName("MyToy1", myToy);
        myCanvas.Children.Add(myToy);
        Canvas.SetTop(myToy, 50);
        Canvas.SetLeft(myToy, 50);
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        DoubleAnimation ani = new DoubleAnimation();
        ani.From = 0;
        ani.To = 359;
        ani.AutoReverse = true;
        ani.RepeatBehavior = RepeatBehavior.Forever;
        ani.Duration = new Duration(TimeSpan.FromSeconds(2));

        Storyboard story = new Storyboard();
        story.Children.Add(ani);
        // Storyboard.SetTargetName(ani, myToy.Name);
        Storyboard.SetTarget(ani, transform);
        Storyboard.SetTargetProperty(ani, new PropertyPath(RotateTransform.AngleProperty));

        story.Begin(this);
    }
}

thanks for any help.


The target of your animation is a MyToy object and your target property is Angle. MyToy doesn't have an angle property though. Solution: Set the RenderTransform (or LayoutTransform) property of MyToy to be a new RotateTransform object. Then use that object (which has the Angle property) as the target of the animation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号