2010-01-25 11 views
8

Sto cercando di scoprire perché il codice seguente non sembra funzionare. Non dà un errore - semplicemente non scala. In realtà sembra funzionare se lo cambio come al mio secondo esempio di codice. Qualcuno ha qualche idea?Applicazione di ScaleTransform animato nel problema di codice

Grazie

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

--- Il seguente funziona ma ho dovuto creare un TransformGroup e riferimento questo attraverso un PropertyChain più complicato ...

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

risposta

22

sono stato in grado di farlo a lavorare per ottimizzare il primo esempio di codice in questo modo:

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

Invece di new PropertyPath(ScaleTransform.ScaleXProperty)), I utilizzato new PropertyPath("RenderTransform.ScaleX")) e ho impostato il target dello storyboard sul pulsante (non su scaleTransform stesso).

Spero che questo aiuti!

3

Ecco un esempio di come animare in due direzioni diverse su ScaleTransform, quando si dispone di un gruppo di trasformazione. La stringa del percorso mostra quale parte viene animata. Inoltre, poiché Canvas è freezable, devi RegisterName. (Non so cosa significhi, ma è obbligatorio)

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