2012-04-02 3 views
2

これらの楕円が大きくなるようにしようとしていますが、アニメーションを開始する方法がわかりません。これはWPFアニメーションでの私の最初の試みであり、どのように動作するのか全く分かりません。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、これは、「WPFのアニメーション」だ、「C#のアニメーション」ではありません。 –

答えて

8

ここにストーリーボードは必要ありません。あなたは本当にストーリーボードでそれを行う必要がある場合だけ

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

を行うには、ストーリーボードに、独立したアニメーション、アニメーションプロパティごとに1つずつ追加する必要があります。また、名前を適用しない場合はSetTargetNameの代わりにSetTargetに電話する必要があります。最後に、あなたがBeginを呼び出すことによって、ストーリーボードを開始する必要があります:

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!ありがとう、私はそのような騒ぎであることは嫌いです。私は簡単な答えがあることを知っていた。これは素晴らしいです。 – miltonjbradley

関連する問題