adv*_*api 3 c# wpf wpf-animation
我想以编程方式使用 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'.
Run Code Online (Sandbox Code Playgroud)
我已经定义了我ColorAnimation的
var storyBoard = new Storyboard();
ColorAnimation colorAnimation = new ColorAnimation
{
From = Colors.Red,
To = Colors.CornflowerBlue,
Duration = TimeSpan.FromSeconds(1),
FillBehavior = FillBehavior.Stop
};
Run Code Online (Sandbox Code Playgroud)
和它的用法我做
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();
}
Run Code Online (Sandbox Code Playgroud)
我必须将什么参数传递给新的PropertyPath?我试图遵循这个例子,但没有任何运气。
你必须指定正确PropertyPath的Color的Brush。
所以代替
PropertyPath colorTargetPath = new PropertyPath(TextBlock.BackgroundProperty);
Run Code Online (Sandbox Code Playgroud)
你必须使用
PropertyPath colorTargetPath =
new PropertyPath("(0).(1)", TextBlock.BackgroundProperty, SolidColorBrush.ColorProperty);
Run Code Online (Sandbox Code Playgroud)
这相当于Storyboard.TargetProperty="(TextBlock.Background).Color"链接答案的 XAML。
现在,它应该工作-至少在现有Brush的TextBlock.Background一个SolidColorBrush。如果没有,您必须适应PropertyPath您的Brush.