Jim Rogers

Lives in Baton Rouge, LA, with two dogs, one cat, and one lovely wife. I'm a lead developer for GCR & Associates.

Katrin and Jim

Month List

WPF - Getting Default Control Templates

by Jim Jun 20, 2008 9:12 AM

YourLameButton

When templating WPF controls, it's easiest to start with the default template defined in the framework - especially if you only need to make minor changes.

This isn't terribly hard to google, but it can't be blogged enough :-)

I'll give you a few options - pick your poison.

Quick and dirty output to the console

StringBuilder sb = new StringBuilder();
using (TextWriter writer = new StringWriter(sb))
{
    System.Windows.Markup.XamlWriter.Save(MyControl.Template, writer);
}
Console.WriteLine(sb.ToString());

The output of that is just a single (long and unreadable) line of xaml; it's nice to have the output on multiple lines with indentation.

Nicely formatted output to the console

System.Text.StringBuilder sb = new System.Text.StringBuilder();

System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
settings.Indent = true; 

using (System.Xml.XmlWriter xmlWriter = System.Xml.XmlWriter.Create(sb, settings))
{
    System.Windows.Markup.XamlWriter.Save(MyControl.Template, xmlWriter);
}

Console.WriteLine(sb.ToString());

Formatted output to a file

System.Xml.XmlWriterSettings settings = new System.Xml.XmlWriterSettings();
settings.Indent = true; 

using (System.Xml.XmlWriter xmlWriter = 
   System.Xml.XmlWriter.Create("C:\\Temp\\Template.xml", settings))
{
    System.Windows.Markup.XamlWriter.Save(MyControl.Template, xmlWriter);
}

The last one is my favorite; it's easy to open the file in Visual Studio and check it out, if you're just looking to see how something was done.

Tags:

Code

Comments (1) -

12/7/2011 2:10:01 PM #

Jan

Thanks, just what i was looking for!

Jan Germany

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading