Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Helper for Extracting Control Template

0.00/5 (No votes)
24 Apr 2014 1  
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:

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

            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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here