2010-01-25 4 views
8

Ich versuche herauszufinden, warum der Code unten scheint nicht zu funktionieren. Es gibt keinen Fehler - es skaliert einfach nicht. Es scheint tatsächlich zu funktionieren, wenn ich es zu meinem zweiten Codebeispiel ändere. Wer hat eine Idee?Anwenden von animierten ScaleTransform in Code-Problem

Dank

public static void StartMouseEnterAnimation(Button button) 
    { 
     Storyboard storyboard = new Storyboard(); 

     ScaleTransform scale = new ScaleTransform(1.0, 1.0, 1, 1); 
     button.RenderTransformOrigin = new Point(0.5, 0.5); 
     button.RenderTransform = scale; 

     DoubleAnimation growAnimation = new DoubleAnimation(); 
     growAnimation.Duration = TimeSpan.FromMilliseconds(300); 
     growAnimation.From = 1; 
     growAnimation.To = 1.8; 
     storyboard.Children.Add(growAnimation); 

     Storyboard.SetTargetProperty(growAnimation, new PropertyPath(ScaleTransform.ScaleXProperty)); 
     Storyboard.SetTarget(growAnimation, scale); 

     storyboard.Begin(); 
    } 

--- Die folgende funktioniert, aber ich hatte eine Transform und Referenz diese durch eine kompliziertere PropertyChain zu schaffen ...

public static void StartMouseEnterAnimation(Button button) 
    {  
     Storyboard storyboard = new Storyboard();    
     ScaleTransform scale = new ScaleTransform(1.0, 1.0, 1, 1); 
     button.RenderTransformOrigin = new Point(0.5, 0.5); 
     TransformGroup myTransGroup = new TransformGroup(); 
     myTransGroup.Children.Add(scale); 
     button.RenderTransform = myTransGroup; 

     DoubleAnimation growAnimation = new DoubleAnimation(); 
     growAnimation.Duration = TimeSpan.FromMilliseconds(100); 
     //growAnimation.From = 1; 
     growAnimation.To = 1.1; 
     storyboard.Children.Add(growAnimation); 

     DependencyProperty[] propertyChain = new DependencyProperty[] 
     { 
      Button.RenderTransformProperty, 
      TransformGroup.ChildrenProperty, 
      ScaleTransform.ScaleXProperty 
     }; 
     string thePath = "(0).(1)[0].(2)"; 
     PropertyPath myPropertyPath = new PropertyPath(thePath, propertyChain); 
     Storyboard.SetTargetProperty(growAnimation, myPropertyPath); 
     Storyboard.SetTarget(growAnimation, button); 

     storyboard.Begin(); 
    } 

Antwort

22

Ich war in der Lage, es zu bekommen zu arbeiten, indem sie wie so Ihr erstes Codebeispiel Tweaking:

public static void StartMouseEnterAnimation(Button button) { 
    Storyboard storyboard = new Storyboard(); 

    ScaleTransform scale = new ScaleTransform(1.0, 1.0); 
    button.RenderTransformOrigin = new Point(0.5, 0.5); 
    button.RenderTransform = scale; 

    DoubleAnimation growAnimation = new DoubleAnimation(); 
    growAnimation.Duration = TimeSpan.FromMilliseconds(300); 
    growAnimation.From = 1; 
    growAnimation.To = 1.8; 
    storyboard.Children.Add(growAnimation); 

    Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.ScaleX")); 
    Storyboard.SetTarget(growAnimation, button); 

    storyboard.Begin(); 
} 

Statt new PropertyPath(ScaleTransform.ScaleXProperty)), ich verwendet new PropertyPath("RenderTransform.ScaleX")), und ich legte das Ziel des Storyboards auf die Schaltfläche (nicht die scaleTransform selbst).

Hoffe, dass hilft!

3

Hier ist ein Beispiel für die Animation in zwei verschiedenen Richtungen auf einer ScaleTransform, wenn Sie eine Transformationsgruppe haben. Die Pfadzeichenfolge zeigt an, welcher Teil animiert wird. Da Canvas Freezable ist, müssen Sie auch RegisterName. (Ich weiß nicht, was das bedeutet, aber es ist erforderlich)

 var storyBoard = new Storyboard(); 
     var group = new TransformGroup(); 
     var scale = new ScaleTransform(Zoom, Zoom); 
     group.Children.Add(scale); 
     group.Children.Add(new TranslateTransform(_translateX,_translateY)); 
     MainCanvas.RenderTransform = group; 

     RegisterName("MainCanvas",MainCanvas); 

     var growAnimation = new DoubleAnimation(); 
     growAnimation.Duration = TimeSpan.FromMilliseconds(1000); 
     growAnimation.From = _oldZoom; 
     growAnimation.To = Zoom; 
     storyBoard.Children.Add(growAnimation); 

     var growAnimation2 = new DoubleAnimation(); 
     growAnimation2.Duration = TimeSpan.FromMilliseconds(1000); 
     growAnimation2.From = _oldZoom; 
     growAnimation2.To = Zoom; 

     storyBoard.Children.Add(growAnimation2); 

     string thePath = "(0).(1)[0].(2)"; // Not used - just to show the syntax 


     Storyboard.SetTargetProperty(growAnimation, new PropertyPath("RenderTransform.Children[0].ScaleX")); 
     Storyboard.SetTargetProperty(growAnimation2, new PropertyPath("RenderTransform.Children[0].ScaleY")); 
     Storyboard.SetTargetName(growAnimation, "MainCanvas"); 
     Storyboard.SetTargetName(growAnimation2,"MainCanvas"); 
     storyBoard.Begin(this); 
Verwandte Themen