2011-05-09 64 views
4

我有一个名为“b”的按钮,我想从黑色变为白色的背景,但它不起作用。颜色动画不起作用

错误:

'System.Windows.Media.Animation.ColorAnimation' animation object cannot be used to animate property 'Background' because it is of incompatible type 'System.Windows.Media.Brush'.

我的代码:

Dim changeColor As New Animation.ColorAnimation 

changeColor.From = Colors.Black 
changeColor.To = Colors.White 
changeColor.Duration = TimeSpan.FromSeconds(0.2) 

Animation.Storyboard.SetTarget(changeColor, b) 
Animation.Storyboard.SetTargetProperty(changeColor, New PropertyPath(BackgroundProperty)) 

Dim sb As New Animation.Storyboard 
sb.Children.Add(changeColor) 
sb.Begin() 

有什么想法?

回答

7

背景是Brush类型,不能用ColorAnimaion动画。然而,的SolidColorBrush有一个颜色属性,所以你可以这样做:

Storyboard.SetTargetProperty(changeColor, new PropertyPath("Background.Color")); 
+0

不工作! :( – Cobold 2011-05-09 19:28:14

+0

其实它的工作!我没有写在正确的外壳。谢谢!:) – Cobold 2011-05-09 19:30:45

19

值得一提的是,同样的问题在XAML使用形式的表达式来解决:

<ColorAnimation Duration="0:0:0.2" From="Black" To="White" 
     Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" 
     Storyboard.TargetName="Body" /> 
+1

感谢这段代码 - 它似乎,而我需要正确设置目标属性,我也不得不添加一个默认的背景颜色到我的边界 – Rob 2012-12-21 00:49:47