2012-04-02 10 views
2

Ich versuche, diese Ellipsen zu wachsen, aber ich kann nicht herausfinden, wie Sie die Animation starten. Dies ist mein erster Versuch bei der WPF-Animation und ich verstehe nicht ganz, wie alles funktioniert.Animieren der Höhe und Breite eines Objekts in C#

private void drawEllipseAnimation(double x, double y) 
{ 
    StackPanel myPanel = new StackPanel(); 
    myPanel.Margin = new Thickness(10); 

    Ellipse e = new Ellipse(); 
    e.Fill = Brushes.Yellow; 
    e.Stroke = Brushes.Black; 
    e.Height = 0; 
    e.Width = 0; 
    e.Opacity = .8; 
    canvas2.Children.Add(e); 
    Canvas.SetLeft(e, x); 
    Canvas.SetTop(e, y); 

    DoubleAnimation myDoubleAnimation = new DoubleAnimation(); 
    myDoubleAnimation.From = 0; 
    myDoubleAnimation.To = 10; 
    myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(5)); 
    myStoryboard = new Storyboard(); 
    myStoryboard.Children.Add(myDoubleAnimation); 
    Storyboard.SetTargetName(myDoubleAnimation, e.Name); 
    Storyboard.SetTargetProperty(myDoubleAnimation, new  PropertyPath(Ellipse.HeightProperty)); 
    Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Ellipse.WidthProperty)); 
} 
+0

FYI, ist dies nicht "C# Animation", dann ist es "WPF Animation". –

Antwort

8

Sie brauchen hier kein Storyboard. Just do

e.BeginAnimation(Ellipse.WidthProperty, myDoubleAnimation); 
e.BeginAnimation(Ellipse.HeightProperty, myDoubleAnimation); 

Wenn Sie es wirklich mit einem Storyboard müssen dies tun, werden Sie separate Animationen, eine pro animierte Eigenschaft, zum Storyboard hinzufügen. Und Sie müssen SetTarget statt SetTargetName aufrufen, wenn Sie keinen Namen anwenden. Schließlich müssen Sie die Storyboard starten, indem Begin Aufruf:

DoubleAnimation widthAnimation = new DoubleAnimation 
{ 
    From = 0, 
    To = 10, 
    Duration = TimeSpan.FromSeconds(5) 
}; 

DoubleAnimation heightAnimation = new DoubleAnimation 
{ 
    From = 0, 
    To = 10, 
    Duration = TimeSpan.FromSeconds(5) 
}; 

Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Ellipse.WidthProperty)); 
Storyboard.SetTarget(widthAnimation, e); 

Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Ellipse.HeightProperty)); 
Storyboard.SetTarget(heightAnimation, e); 

Storyboard s = new Storyboard(); 
s.Children.Add(widthAnimation); 
s.Children.Add(heightAnimation); 
s.Begin(); 
+0

GENIUS! Danke, ich hasse es, so ein Noob zu sein. Ich wusste, dass es eine einfache Antwort gab. Das funktioniert großartig. – miltonjbradley

Verwandte Themen