Archive for August, 2009

Cloning an element on the Visual Tree

August 15, 2009 1 comment

Sometime is it is necessary to make a copy of an element on the visual tree and the insert it somewhere for use.  Unfortunately, copying an element and inserting it in another location is not allowed.  Instead of doing this all from scratch and creating a new instance of the element that you want.  You can always resort to other techniques.  I will be presenting one such technique.  The approach I use involves using the DataContractSerializer.  This is a built-in class in the System.Runtime.Serialization namespace.  It is very powerful and is used ubiquitously throughout WCF.  Here is a screenshot of the CloneFactory I created:

As you can tell, it is a very succinct and short piece of code.  Yet this code is very powerful.  It does in memory serialization and only as one level deep.  You could always refactor this a little to make it recursive or to define the number of levels deep you wished to copy.  This would allow you to have deep copy to the level that you so desired.

Hope this helps….

Categories: English Tags: ,