Reputation: 23
Is there anyway to extract the underlying xaml from a control.
IE. I have a textbox named fooBox. Can I get xaml back from the textbox at runtime that represents the textbox?
Upvotes: 2
Views: 1166
Reputation: 5027
For Silverlight, I ran across this blog post by Rob Relyea that referred to a Silverlight xaml serializer created by David Poll. Huge kudos to David Poll. (Downloads Page).
Usage
UiXamlSerializer uxs = new UiXamlSerializer();
string text = uxs.Serialize(this.gridToSerialize);
Upvotes: 1
Reputation: 10048
This shows you the full lifecycle (from control to XAML back to control). As you can see,
string s = XamlWriter.Save(value);
is the interesting part you might care about.
/// <summary>
/// Clones a given UIElement. Please note that any events, animations, etc
/// on the source item may not carry over to the cloned object.
/// </summary>
/// <param name="value">UIElement to clone.</param>
/// <returns>A shallow clone of the source element.</returns>
public static UIElement CloneUIElement(UIElement value)
{
if (value == null)
{
return null;
}
string s = XamlWriter.Save(value);
StringReader stringReader = new StringReader(s);
XmlReader xmlReader = XmlTextReader.Create(stringReader, new XmlReaderSettings());
return (UIElement)XamlReader.Load(xmlReader);
}
Upvotes: 3
Reputation: 74682
Yes - Josh Smith does this in his Mole tool: http://www.codeproject.com/KB/WPF/MoleForWPF.aspx
Upvotes: 0