2011-10-24 23 views
1

グリッド内の矩形の色をプログラムで変更するにはどうすればよいですか?WPF:プログラムでグリッド内の矩形の色をアニメ化する

 ColorAnimation myColorAnimation = new ColorAnimation(); 
     myColorAnimation.From = Colors.Red; 
     myColorAnimation.To = Colors.Blue; 
     myColorAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(500)); 
     myColorAnimation.AutoReverse = false; 

     myStoryboard = new Storyboard(); 
     myStoryboard.Children.Add(myColorAnimation); 
     Storyboard.SetTargetName(myColorAnimation, ?); // What do I put here 
     Storyboard.SetTargetProperty(myColorAnimation, new PropertyPath //What do I put here? 
+0

あなたの四角形とそれに配置されているグリッドのxamlコードを表示できますか? –

答えて

0

OK、私はこれを行う方法を見つけました:

私は、各グリッド項目のストーリーボードを作成します。

List<Storyboard> _animatableGridRectToGray = new List<Storyboard>(); 
    List<Storyboard> _animatableGridRectToWhite = new List<Storyboard>(); 

と移入:

private void initAnimatableGridRectangles() 
{ 
    int index = 1; 
    foreach (Rectangle rect in SequenceGrid.Children) 
    { 
     SolidColorBrush tempBrush = new SolidColorBrush(); 
     rect.Fill = tempBrush; 
     string brushName = "Brush" + index; 
     _gridBrushes.Add(brushName, tempBrush); 
     this.RegisterName(brushName, tempBrush); 

     Storyboard tempSBToGray = new Storyboard(); 
     Storyboard tempSBToWhite = new Storyboard(); 
     ColorAnimation tempColAnimToGray = getAnimToGray(); 
     ColorAnimation tempColAnimToWhite = getAnimToWhite(); 
     tempSBToGray.Children.Add(tempColAnimToGray); 
     tempSBToWhite.Children.Add(tempColAnimToWhite); 
     Storyboard.SetTargetName(tempColAnimToGray, brushName); 
     Storyboard.SetTargetName(tempColAnimToWhite, brushName); 
     Storyboard.SetTargetProperty(tempColAnimToGray, new PropertyPath(SolidColorBrush.ColorProperty)); 
     Storyboard.SetTargetProperty(tempColAnimToWhite, new PropertyPath(SolidColorBrush.ColorProperty)); 
     _animatableGridRectToGray.Add(tempSBToGray); 
     _animatableGridRectToWhite.Add(tempSBToWhite); 
     index++; 
    } 
} 


private ColorAnimation getAnimToGray() 
{ 
    ColorAnimation colAnim = new ColorAnimation(); 
    colAnim.To = Colors.Gray; 
    colAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500)); 
    colAnim.AutoReverse = false; 
    return colAnim; 
} 

private ColorAnimation getAnimToWhite() 
{ 
    ColorAnimation colAnim = new ColorAnimation(); 
    colAnim.To = Colors.White; 
    colAnim.Duration = new Duration(TimeSpan.FromMilliseconds(500)); 
    colAnim.AutoReverse = false; 
    return colAnim; 
} 

その後、私ができますアニメーションのように:

_animatableGridRectToGray[index].Begin(this); 
関連する問題