Click here to Skip to main content
13,056,238 members (79,856 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 24 Apr 2014

Helper for Extracting Control Template

, 24 Apr 2014
Rate this:
Please Sign up or sign in to vote.
Attached behavior that saves control's template in a file as XAML


I decided to write a little helper that saves control's template in an XAML file. I am a lazy person, so next time I need a template, I don't want to modify too much code. The class is easy to use, all what you need is to modify XAML of the control you are interested in. For example, you have some ThirdPartyControl defined in XAML:

<ThirdPartyControl SomeProp="SomeValue"/>

Now, if you want to save the control's template in file template.xaml, just add the following attribute:

<ThirdPartyControl ExtractorNamespace:TemplateExtractor.FileName="template.xaml" SomeProp="SomeValue"/> 

When your control loads, you'll have your template saved.

The Code

It's a class that contains attached property. On property assignment, it attaches onload handler for the control which saves the template.

public class TemplateExtractor
    public static readonly DependencyProperty FileNameProperty;

    static TemplateExtractor()
        var meta = new PropertyMetadata( null, OnFileNameChanged );

        FileNameProperty = DependencyProperty.RegisterAttached
        ( "FileName", typeof( string ), typeof( TemplateExtractor ), meta );

    private static void OnFileNameChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
        var control = d as Control;

        if( control == null ) return;

        control.Loaded += OnControlLoaded;

    private static void OnControlLoaded( object sender, RoutedEventArgs e )
        var control = sender as Control;

        if( control == null || control.Template == null ) return;

        control.Loaded -= OnControlLoaded;

        string fileName = GetFileName( control );

        if( fileName == null ) return;

            using( XmlWriter xmlWriter = XmlWriter.Create( fileName, new XmlWriterSettings { Indent = true } ) )
                XamlWriter.Save( control.Template, xmlWriter );
        catch { /* Anyway it's debugging code */ }

    public static string GetFileName( FrameworkElement frameworkElement )
        return (string)frameworkElement.GetValue( FileNameProperty );

    public static void SetFileName( FrameworkElement frameworkElement, string value )
        frameworkElement.SetValue( FileNameProperty, value );


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Cuba Cubicula
Software Developer
Canada Canada
.Net Developer

You may also be interested in...


Comments and Discussions

GeneralThanks Pin
Zyx Maiden2-May-14 13:38
memberZyx Maiden2-May-14 13:38 
GeneralMy vote of 5 Pin
r_hyde24-Apr-14 16:03
memberr_hyde24-Apr-14 16:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170728.2 | Last Updated 24 Apr 2014
Article Copyright 2014 by Cuba Cubicula
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid