Clone a Visual Element in WPF
I came across this interesting snippet while looking to clone a list item during a drag drop operation. Perhaps there’s a better more efficient way, but this worked nicely in a pinch:
[csharp]
public Object Clone(Object o)
{
string xamlCode = XamlWriter.Save(o);
return XamlReader.Load(new XmlTextReader(new StringReader(xamlCode)));
}
[/csharp]
it just serializes the object graph to a string and re-hydrates it to construct the copy. You have to love two lines of code that solves a problem in a hurry.