silverlightbrushes

What is the best way to copy / clone brushes?


Before drawing a shape on a canvas I have a preview that displays how the shape will look. I can adjust the opacity and then draw the shape. I may then wish to draw a second shape with a different opacity. My problem is that altering the opacity of the preview also alters the opacity of the shape that I have already drawn.

This has led me to believe that I need to create a copy of the brush used for the preview each time before drawing the shape.

There are various different brushes and for example, the gradient brushes require making a copy of the not just the gradient stop collection, but a new gradient stop for each gradient stop in the to-be-copied collection.

Am I down the right track here or should I be doing something else? Should I be copying or cloning? Would an extension method be the best way to go? Thoughts please.


Solution

  • What you need is cloning, it would be easy in wpf with XamlWriter/Reader, unfortunately you cannot do it in Silverlight. An extension method on Brush that makes a deep copy would work fine in your case though. You will have to handle the different brush type separately but it should not be an issue as there aren't that many.