2010-01-25 64 views
8

我想知道爲什麼下面的代碼似乎不起作用。它不會給出錯誤 - 它不會縮放。它實際上似乎工作,如果我改變它作爲我的第二個代碼示例。 任何人有任何想法?在代碼問題中應用動畫ScaleTransform

感謝

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

---下面的工作,但我不得不通過更復雜的PropertyChain創建的TransformGroup和參考這個...

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

回答

22

我能得到它通過調整您的第一個代碼示例像這樣工作:

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

而不是new PropertyPath(ScaleTransform.ScaleXProperty)),I使用new PropertyPath("RenderTransform.ScaleX")),我將故事板的目標設置爲按鈕(而不是scaleTransform本身)。

希望有幫助!

3

下面是如何在ScaleTransform上創建兩個不同方向的動畫的示例,當您有變換組時。路徑字符串顯示正在動畫的部分。另外,由於Canvas可凍結,因此您必須登錄RegisterName。 (我不知道這是什麼意思,但它是必需的)

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