Jim's Software |  Jim and Katrin

Random development notes

WPF - Getting Default Control Templates

6/20/2008  10: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.

Labels: ,

Comments:

Post a Comment





<< Home