Search code examples
.netwpfanimationclonebrush

How to Clone animated Brush?


I have an animated Brush object and i want to Clone this brush.

ColorAnimation ani = new ColorAnimation(Colors.White, TimeSpan.FromSeconds(1))
{ RepeatBehavior = RepeatBehavior.Forever, AutoReverse = true };

SolidColorBrush brush1 = new SolidColorBrush(Colors.Black);
brush1.BeginAnimation(SolidColorBrush.ColorProperty, ani);

SolidColorBrush brush2 = brush1.Clone();

// brush2 is not an animated Brush

if (!brush2.HasAnimatedProperties)
    MessageBox.Show("I don't want this!");

As MSDN Library says (Brush.Clone method):

Creates a modifiable clone of this Brush, making deep copies of this object's values. When copying dependency properties, this method copies resource references and data bindings (but they might no longer resolve) but not animations or their current values.

So, what's the best way to clone my animated brush? Thanks.


Solution

  • You need to recreate the animation on the clone. There is no other way.