How to make a deep copy of UIElement?

So, I have a print component that serves for a Silverlight application. Other modules in this program have the ability to signal the print component and pass it a UIElement , which then prints the component on the screen. All is well and good. The problem arises when I try to manipulate a user interface element to better format it so that it matches the size of the paper selected by the user or something for that matter; it seems that the UI element that is being transmitted is often the same instance of what is on the screen, and the screen element changes itself to match the print-only changes I made. At the moment, I can manually save the previous values, make changes and restore the previous values, but it would be easier / more reliable / more efficient / more flexible if I had a way, taking into account the user interface element, to make a copy of the element and freely manipulate it without worrying about changes or states of the original interface element. How can I programmatically copy an instance of a UI element so that I have another instance with the same visual appearance?

+4
source share
2 answers

I know two ways you can try:

Save the object in the xaml line and recreate it. ( XamlWriter.Save and XamlReader.Parse )

Save the object using the serializer in memystream and recreate it from this - it is possible that not all objects are marked as serializable, so another option may be the one that will be used.

This may seem a bit - but there are not many ways to create a deep copy, and not some standard C # method that I know of.

+1
source

If you have already encountered the problem of manually saving the previous values, it seems that you are pretty close to simply creating a new instance of the object and setting its properties.

If a user interface element has been marked as serializable, you can easily clone the object.

+1
source

All Articles