Geeks With Blogs

News The Code Project

I am from Munshigonj, Bangladesh. For me, programming is a passion first, a hobby second, and a career third. My Primary Blog:
Razan Paul Blog Explaining thoughts and findings is a great way to learn

We can make a deep copy of  a WPF object using XamlWriter and XamlReader. Here the XamlWriter.Save is used to serialize the contents of a WPF object into xaml string.  XamlReader.Load is used to parse XAML string into a WPF object. To make deep copy of an wpf UIelement , you can use the following method.

   1: public UIElement DeepCopy(UIElement element)
   2:    {
   3:        string shapestring = XamlWriter.Save(element);
   4:        StringReader stringReader = new StringReader(shapestring);
   5:        XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
   6:        UIElement DeepCopyobject = (UIElement)XamlReader.Load(xmlTextReader);
   7:        return DeepCopyobject;
   8:    }

If you would like to make deep copy of  WPF  objects without XamlWriter, you have to use reflection .For that you have to navigate recursively inside the type to get all Dependency Properties and Dependency Objects as well as plain .net properties.

Posted on Friday, September 18, 2009 8:00 PM | Back to top

Comments on this post: How can we make a deep copy of a WPF object?

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Razan | Powered by: