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?
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?
Yes - Josh Smith does this in his Mole tool: http://www.codeproject.com/KB/WPF/MoleForWPF.aspx
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);
}