JRobbers
JRobbers

Reputation: 23

Any way to extract underlying Xaml?

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

Answers (3)

Jason
Jason

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

Erich Mirabal
Erich Mirabal

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

Ana Betts
Ana Betts

Reputation: 74682

Yes - Josh Smith does this in his Mole tool: http://www.codeproject.com/KB/WPF/MoleForWPF.aspx

Upvotes: 0

Related Questions