I'm not quite sure if it works in all cases, but it does work for Canvas.
DataTemplate GetElementAsTemplate(FrameworkElement element) {
StringBuilder builder = new StringBuilder();
builder.AppendFormat(
"
XamlWriter.Save(element));
using(MemoryStream stream = new MemoryStream()) {
using(StreamWriter writer = new StreamWriter(stream)) {
writer.Write(builder.ToString());
writer.Flush();
stream.Position = 0;
DataTemplate template = (DataTemplate)XamlReader.Load(stream);
return template;
}
}
}