[英]'ColorAnimation' animation object cannot be used to animate property 'Background' because it is of incompatible type 'System.Windows.Media.Brush'
I'm tring to use a ColorAnimation programmatically to animate a cell, but I got this when I perform storyboard.Begin()
我想以编程方式使用 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'.
I've defined my ColorAnimation
as我已经将我的
ColorAnimation
定义为
var storyBoard = new Storyboard();
ColorAnimation colorAnimation = new ColorAnimation
{
From = Colors.Red,
To = Colors.CornflowerBlue,
Duration = TimeSpan.FromSeconds(1),
FillBehavior = FillBehavior.Stop
};
and on it's usage I do和它的用法我做
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();
}
What paramater do I have to pass to the new PropertyPath
?我必须将什么参数传递给新的
PropertyPath
? I've tried to follow this example but without any luck.我试图遵循这个例子,但没有任何运气。
You have to specify the correct PropertyPath
to the Color
of the Brush
.您必须为
Brush
的Color
指定正确的PropertyPath
。
So instead of所以代替
PropertyPath colorTargetPath = new PropertyPath(TextBlock.BackgroundProperty);
you have to use你必须使用
PropertyPath colorTargetPath =
new PropertyPath("(0).(1)", TextBlock.BackgroundProperty, SolidColorBrush.ColorProperty);
This is the equivalent of Storyboard.TargetProperty="(TextBlock.Background).Color"
in the XAML of your linked answer.这相当于链接答案的 XAML 中的
Storyboard.TargetProperty="(TextBlock.Background).Color"
。
Now it should work - at least if the existing Brush
of the TextBlock.Background
is a SolidColorBrush
.现在它应该可以工作了 - 至少如果
TextBlock.Background
的现有Brush
是SolidColorBrush
。 If not, you have to adapt the PropertyPath
to your type of Brush
.如果没有,您必须使
PropertyPath
适应您的Brush
类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.