2013-10-20 84 views
3

我已經在一個ControlTemplate.Resources如下:動畫顏色屬性有不同的畫筆

<ColorAnimation 
    Storyboard.TargetName="border" 
    Storyboard.TargetProperty="Background.(SolidColorBrush.Color)" 
    To="Orange" 
    Duration="0:0:0.2" /> 

它的工作原理沒事,如果我想改變橙色原始背景是純色。但是當我的原始背景是LinearGradientBrush時,我也想要做這個工作。在這第二種情況下,動畫試圖改變屬性,沒有任何反應。

如何指定一個替換背景的動畫,而不管它是什麼類型?

回答

4

如果您BackgroundLinearGradientBrush,那麼你將有動畫每個GradientStopColor你想即橙在這種情況下:

 <Storyboard x:Key="Storyboard1"> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Background.(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="border"> 
      <EasingColorKeyFrame KeyTime="0:0:2" Value="Orange"/> 
     </ColorAnimationUsingKeyFrames> 
     <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Background.(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="border"> 
      <EasingColorKeyFrame KeyTime="0:0:2" Value="Orange"/> 
     </ColorAnimationUsingKeyFrames> 
    </Storyboard> 

但是,如果你想Animate整個Brush無論其類型,然後你將不得不創建你自己的動畫。我已經創建了自己的BrushAnimation類動畫刷

public class BrushAnimation : AnimationTimeline 
    { 
     static BrushAnimation() 
     { 
      FromProperty = DependencyProperty.Register("From", typeof(Brush), 
       typeof(BrushAnimation),new PropertyMetadata(new SolidColorBrush())); 

      ToProperty = DependencyProperty.Register("To", typeof(Brush), 
       typeof(BrushAnimation), new PropertyMetadata(new SolidColorBrush())); 
     } 

     public override Type TargetPropertyType 
     { 
      get 
      { 
       return typeof(Brush); 
      } 
     } 

     protected override System.Windows.Freezable CreateInstanceCore() 
     { 
      return new BrushAnimation(); 
     } 

     public static readonly DependencyProperty FromProperty; 
     public Brush From 
     { 
      get 
      { 
       return (Brush)GetValue(BrushAnimation.FromProperty); 
      } 
      set 
      { 
       SetValue(BrushAnimation.FromProperty, value); 
      } 
     } 

     public static readonly DependencyProperty ToProperty; 
     public Brush To 
     { 
      get 
      { 
       return (Brush)GetValue(BrushAnimation.ToProperty); 
      } 
      set 
      { 
       SetValue(BrushAnimation.ToProperty, value); 
      } 
     } 


     public override object GetCurrentValue(object defaultOriginValue, 
      object defaultDestinationValue, AnimationClock animationClock) 
     { 
      Brush fromVal = ((Brush)GetValue(BrushAnimation.FromProperty)); 
      Brush toVal = ((Brush)GetValue(BrushAnimation.ToProperty)); 

      SolidColorBrush solid = toVal as SolidColorBrush; 

      if(fromVal is LinearGradientBrush) 
      { 
       LinearGradientBrush brush = fromVal as LinearGradientBrush; 
       LinearGradientBrush newBrush = new LinearGradientBrush(); 
       foreach(var stop in brush.GradientStops) 
       { 
        ColorAnimation animation = new ColorAnimation(stop.Color,solid.Color,this.Duration); 
        Color color = animation.GetCurrentValue(stop.Color, solid.Color, animationClock); 
        newBrush.GradientStops.Add(new GradientStop(color,stop.Offset)); 
       } 

       return newBrush; 
      } 
      else 
      { 
       SolidColorBrush brush = fromVal as SolidColorBrush; 
       SolidColorBrush newsolid = new SolidColorBrush(); 
       ColorAnimation solidAnimation = new ColorAnimation(brush.Color, solid.Color, this.Duration); 
       newsolid.Color = solidAnimation.GetCurrentValue(brush.Color, solid.Color, animationClock); 

       return newsolid; 

      } 

     } 

,我用這個動畫我的窗前

<Storyboard x:Key="MyStoryBoard" RepeatBehavior="Forever" AutoReverse="True"> 
     <local:BrushAnimation Storyboard.TargetName="Canvas1" 
         Storyboard.TargetProperty = "(Canvas.Background)" 
         To="Orange" Duration="0:0:5"/> 
    </Storyboard> 

在動畫Canvas.Background,您可以使用靜態資源設置動畫的From財產或者將其設置爲代碼隱藏中的控制背景,如:

public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
      ((BrushAnimation) ((Storyboard) Resources["SolidStoryBoard"]).Children[0]).From = Canvas1.Background; 
     } 
    } 
+0

問題是我不知道它是什麼。這是一個可能具有任何一種背景的按鈕。 –

+0

我已經創建了動畫類來處理這個問題。測試它..效果不錯:) – Nitin

+0

非常好的解決方案,謝謝。 –