简体   繁体   English

'ColorAnimation' 动画对象不能用于为属性 'Background' 设置动画,因为它是不兼容的类型 'System.Windows.Media.Brush'

[英]'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 .您必须为BrushColor指定正确的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的现有BrushSolidColorBrush If not, you have to adapt the PropertyPath to your type of Brush .如果没有,您必须使PropertyPath适应您的Brush类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将BitmapImage转换为System.Windows.Media.Brush - Convert BitmapImage to System.Windows.Media.Brush 将System.Windows.Media.Brush转换为System.Drawing.Brush - Convert System.Windows.Media.Brush to System.Drawing.Brush 将system.windows.media.brush转换为Hex颜色代码 - Converting system.windows.media.brush to Hex color code C#如何将system.windows.media.brush转换为system.drawing.brush - C# how to convert system.windows.media.brush to system.drawing.brush 将Color作为#XXXXXX等字符串转换为System.Windows.Media.Brush的最简单方法 - Simpliest way to convert a Color as a string like #XXXXXX to System.Windows.Media.Brush DoubleAnimationUsingKeyFrames 动画不能用于为属性 RenderTransform 设置动画,因为它是不兼容的 Transform 类型 - DoubleAnimationUsingKeyFrames animation cannot be used to animate property RenderTransform 'cause it is of incompatible type Transform WPF colorAnimation 边框画笔颜色抛出“System.Windows.Markup.XamlParseException” - WPF colorAnimation border brush color throws 'System.Windows.Markup.XamlParseException' 无法将类型'Windows.UI.Color'隐式转换为'Windows.UI.Xaml.Media.Brush' - Cannot implicitly convert type 'Windows.UI.Color' to 'Windows.UI.Xaml.Media.Brush' “System.Windows.Controls.ControlTemplate”类型的对象不能应用于需要“System.Windows.Style”类型的属性。 - An object of the type “System.Windows.Controls.ControlTemplate” cannot be applied to a property that expects the type “System.Windows.Style” 由于对象已密封或冻结,因此无法设置颜色属性的动画 - Cannot animate the color property because the object is sealed or frozen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM