<p>Anybody have a good example how to deep clone a WPF object, preserving databindings?</p>

<hr>

<p>The marked answer is the first part.</p>

<p>The second part is that you have to create an ExpressionConverter and inject it into the serialization process.  Details for this are here:<br>
<a href="http://www.codeproject.com/KB/WPF/xamlwriterandbinding.aspx?fid=1428301&amp;df=90&amp;mpp=25&amp;noise=3&amp;sort=Position&amp;view=Quick&amp;select=2801571" rel="noreferrer">http://www.codeproject.com/KB/WPF/xamlwriterandbinding.aspx?fid=1428301&amp;df=90&amp;mpp=25&amp;noise=3&amp;sort=Position&amp;view=Quick&amp;select=2801571</a></p>

[{'answer_id': 32575, 'author': 'Arcturus', 'author_id': 900, 'author_profile': 'https://Stackoverflow.com/users/900', 'pm_score': 0, 'selected': False, 'text': '<p>How about:</p>\n\n<pre><code>    public static T DeepClone&lt;T&gt;(T from)\n    {\n        using (MemoryStream s = new MemoryStream())\n        {\n            BinaryFormatter f = new BinaryFormatter();\n            f.Serialize(s, from);\n            s.Position = 0;\n            object clone = f.Deserialize(s);\n\n            return (T)clone;\n        }\n    }\n</code></pre>\n\n<p>Of course this deep clones any object, and it might not be the fastest solution in town, but it has the least maintenance... :)</p>\n'}, {'answer_id': 33036, 'author': 'Alan Le', 'author_id': 1133, 'author_profile': 'https://Stackoverflow.com/users/1133', 'pm_score': 7, 'selected': True, 'text': "<p>The simplest way that I've done it is to use a XamlWriter to save the WPF object as a string. The Save method will serialize the object and all of its children in the logical tree. Now you can create a new object and load it with a XamlReader.</p>\n\n<p>ex:\nWrite the object to xaml (let's say the object was a Grid control):</p>\n\n<pre><code>string gridXaml = XamlWriter.Save(myGrid);\n</code></pre>\n\n<p>Load it into a new object:</p>\n\n<pre><code>StringReader stringReader = new StringReader(gridXaml);\nXmlReader xmlReader = XmlReader.Create(stringReader);\nGrid newGrid = (Grid)XamlReader.Load(xmlReader);\n</code></pre>\n"}, {'answer_id': 4973369, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': False, 'text': '<p>In .NET 4.0, the new xaml serialization stack makes this MUCH easier.</p>\n\n<pre><code>var sb = new StringBuilder();\nvar writer = XmlWriter.Create(sb, new XmlWriterSettings\n{\n    Indent = true,\n    ConformanceLevel = ConformanceLevel.Fragment,\n    OmitXmlDeclaration = true,\n    NamespaceHandling = NamespaceHandling.OmitDuplicates, \n});\nvar mgr = new XamlDesignerSerializationManager(writer);\n\n// HERE BE MAGIC!!!\nmgr.XamlWriterMode = XamlWriterMode.Expression;\n// THERE WERE MAGIC!!!\n\nSystem.Windows.Markup.XamlWriter.Save(this, mgr);\nreturn sb.ToString();\n</code></pre>\n'}, {'answer_id': 14740448, 'author': 'John Zabroski', 'author_id': 1040437, 'author_profile': 'https://Stackoverflow.com/users/1040437', 'pm_score': 3, 'selected': False, 'text': '<p>There are some great answers here.  Very helpful.  I had tried various approaches for copying Binding information, including the approach outlined in <a href="http://pjlcon.wordpress.com/2011/01/14/change-a-wpf-binding-from-sync-to-async-programatically/" rel="noreferrer">http://pjlcon.wordpress.com/2011/01/14/change-a-wpf-binding-from-sync-to-async-programatically/</a> but the information here is the best on the Internet!</p>\n\n<p>I created a re-usable extension method for dealing with InvalidOperationException “Binding cannot be changed after it has been used.”  In my scenario, I was maintaining some code somebody wrote, and after a major DevExpress DXGrid framework upgrade, it no longer worked.  The following solved my problem perfectly.  The part of the code where I return the object could be nicer, and I will re-factor that later.</p>\n\n<pre><code>/// &lt;summary&gt;\n/// Extension methods for the WPF Binding class.\n/// &lt;/summary&gt;\npublic static class BindingExtensions\n{\n    public static BindingBase CloneViaXamlSerialization(this BindingBase binding)\n    {\n        var sb = new StringBuilder();\n        var writer = XmlWriter.Create(sb, new XmlWriterSettings\n        {\n            Indent = true,\n            ConformanceLevel = ConformanceLevel.Fragment,\n            OmitXmlDeclaration = true,\n            NamespaceHandling = NamespaceHandling.OmitDuplicates,\n        });\n        var mgr = new XamlDesignerSerializationManager(writer);\n\n        // HERE BE MAGIC!!!\n        mgr.XamlWriterMode = XamlWriterMode.Expression;\n        // THERE WERE MAGIC!!!\n\n        System.Windows.Markup.XamlWriter.Save(binding, mgr);\n        StringReader stringReader = new StringReader(sb.ToString());\n        XmlReader xmlReader = XmlReader.Create(stringReader);\n        object newBinding = (object)XamlReader.Load(xmlReader);\n        if (newBinding == null)\n        {\n            throw new ArgumentNullException("Binding could not be cloned via Xaml Serialization Stack.");\n        }\n\n        if (newBinding is Binding)\n        {\n            return (Binding)newBinding;\n        }\n        else if (newBinding is MultiBinding)\n        {\n            return (MultiBinding)newBinding;\n        }\n        else if (newBinding is PriorityBinding)\n        {\n            return (PriorityBinding)newBinding;\n        }\n        else\n        {\n            throw new InvalidOperationException("Binding could not be cast.");\n        }\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/32541', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']