2011-10-24 3 views
1

Comment modifier la couleur d'un rectangle dans ma grille?WPF: Animation programmée de la couleur Rectangle dans la grille

 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

pouvez-vous afficher le code xaml pour votre rectangle et la grille dans laquelle il est placé? –

Répondre

0

OK, j'ai trouvé une façon de le faire:

-je créer un storyboard pour chaque élément Grille:

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

et remplir:

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; 
} 

Ensuite, je peux animer comme suit:

_animatableGridRectToGray[index].Begin(this); 
Questions connexes