【发布时间】:2016-10-18 15:37:26
【问题描述】:
我正在尝试以编程方式使用 ColorAnimation 来为单元格设置动画,但是当我执行 storyboard.Begin() 时我得到了这个
'System.Windows.Media.Animation.ColorAnimation' animation object cannot be used to animate property 'Background' because it is of incompatible type 'System.Windows.Media.Brush'.
我已将我的ColorAnimation 定义为
var storyBoard = new Storyboard();
ColorAnimation colorAnimation = new ColorAnimation
{
From = Colors.Red,
To = Colors.CornflowerBlue,
Duration = TimeSpan.FromSeconds(1),
FillBehavior = FillBehavior.Stop
};
关于它的使用情况
if (column.UniqueName != "_ID")
{
var animation = animationMapping[column.UniqueName].Animation;
var storyboard = animationMapping[column.UniqueName].Storyboard;
Storyboard.SetTarget(animation, cell.Content as TextBlock);
//Storyboard.SetTargetProperty(animation,
// new PropertyPath((TextBlock.Foreground).Color"));
PropertyPath colorTargetPath = new PropertyPath(TextBlock.BackgroundProperty);
Storyboard.SetTargetProperty(animation, colorTargetPath);
storyboard.Begin();
}
我必须将什么参数传递给新的PropertyPath?我尝试关注this example,但没有任何运气。
【问题讨论】:
-
使用这个
PropertyPath colorTargetPath = new PropertyPath("(TextBlock.Background).(SolidColorBrush.Color)", null);。 -
@AnjumSKhan 你真的试过你的代码吗?如果我运行你的代码,我会得到一个
InvalidOperationException和消息Cannot resolve all property references in the property path '(TextBlock.Background).(SolidColorBrush.Color)'. Verify that applicable objects support the properties.。所以很明显这段代码不起作用。 (请参阅我的工作答案。)
标签: c# wpf wpf-animation