2

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.

gliderkite
  • 8,828
  • 6
  • 44
  • 80

2 Answers2

2

A solid approach could be extending the Brush class and implement ICloneable in your derived class.

Your custom Clone method can then handle the cloning of whatever you need to be cloned.

Matteo Mosca
  • 7,380
  • 4
  • 44
  • 80
  • Could you make me an example of how my custom Clone method is implemented? – gliderkite May 12 '12 at 19:09
  • Well, ICloneable is a rather simple interface. It exposes a single method Clone, so when you implement it, you just put the code that takes properties/fields from the source object and creates copies of them, and then returns a new object with the copied stuff. The implementation details really depend on what you need to do and how you want to do it. – Matteo Mosca May 12 '12 at 19:12
  • @usr You can't extend SolidColorBrush as its sealed, but you can definitely extend Brush. Check better. – Matteo Mosca May 12 '12 at 19:13
  • True. Still you can't do anything with your derived brush. It doesn't work because the native part of WPF does not know how to draw it. For that reason I thought they would have made the ctor internal. Deriving it makes no sense. – usr May 12 '12 at 19:47
  • @MatteoMosca if you think this is the way just make the example considering my code. Just clone a SolidColorBrush with Color property and (obviously) the animation. – gliderkite May 12 '12 at 19:58
  • 1
    Dude, I'm not doing your homework for you. I provided some theory and an idea on how to proceed. Given that SolidColorBrush is sealed, you can't alter it in any way. So either construct your own brush or take piece by piece, manually, what you need from your SolidColorBrush instance and do whatever you need to do. – Matteo Mosca May 12 '12 at 20:06
  • Without proof? Right. So, basically, basic OOP concepts like inheritance, polymorphysm, etc aren't enough proof. Implementing an interface method surely needs a lot of elaboration because is something so foreign... or maybe not. The concept of "sealed" class is obviously so obscure and remote.. again, probably not. The ICloneable interface is so complex, so many methods to implement... no wait, it's not like that. So what's left? Hint: the actual code you need to write to wire things up. Which is basically what you're asking other people to do for you. Not going to happen. – Matteo Mosca May 12 '12 at 20:25
  • The time that you used to write this comment would be enough to make a small example. The only thing to clarify is the notion of "good answer". – gliderkite May 12 '12 at 20:31
  • A sample wouldn't be as useful as the bits of knowledge and hints that will point you in the right direction so you'll be able to write your own code. The point of your post isn't the Brush class, is that you don't understand how inheritance, sealed classes and a simple interface as ICloneable work. I'd take the hints and try to fill your gaps in knowledge. Do that, and the solution to the problem at hand will come to you in a natural way. – Matteo Mosca May 12 '12 at 20:49
2

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

usr
  • 168,620
  • 35
  • 240
  • 369