Click here to Skip to main content
15,885,309 members
Articles / Desktop Programming / WPF
Tip/Trick

Helper for Extracting Control Template

Rate me:
Please Sign up or sign in to vote.
4.87/5 (7 votes)
24 Apr 2014CPOL 7.6K   5   2
Attached behavior that saves control's template in a file as XAML

Introduction

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:

XML
<ThirdPartyControl SomeProp="SomeValue"/>

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

XML
<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.

C#
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;

        try
        {
            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 );
    }
}

License

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


Written By
Software Developer
Canada Canada
.Net Developer

Comments and Discussions

 
GeneralThanks Pin
Zyx Maiden2-May-14 13:38
Zyx Maiden2-May-14 13:38 
GeneralMy vote of 5 Pin
r_hyde24-Apr-14 16:03
r_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.