This is going to be a very short tip, but it is one that could save you a lot of time and lots of nerves!

Every WPF Developer has probably faced this issue before. You want to change the default template of a control and need the default template of a control in XAML.  Where can you get the default template? There are multiple ways:

  1. Find the correct MSDN page for your framework version
  2. Use a 3rd party tool
  3. Use Expression Blend

In my opinion, none of these solutions are very convenient. I always end up either not finding the correct version, not having blend installed, etc…

Alas, there is a much easier way. You can use the XamlWriter class to serialize the default template of your control:

public static void SaveDefaultTemplate()
{
    var control = Application.Current.FindResource(typeof(ProgressBar));
    using (XmlTextWriter writer = new XmlTextWriter(@"defaultTemplate.xml", System.Text.Encoding.UTF8))
    {
        writer.Formatting = Formatting.Indented;
        XamlWriter.Save(control, writer);
    }
}

This will produce the following file for the ProgressBar in the sample code:

<Style TargetType="Button" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib">
  <Style.BasedOn>
    <Style TargetType="ButtonBase">
      <Style.Resources>
        <ResourceDictionary />
      </Style.Resources>
      <Setter Property="FrameworkElement.FocusVisualStyle">
        <Setter.Value>
          <Style TargetType="IFrameworkInputElement">
            <Style.Resources>
              <ResourceDictionary />
            </Style.Resources>
            <Setter Property="Control.Template">
              <Setter.Value>
                <ControlTemplate>
                  <Rectangle Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2" Margin="2,2,2,2" SnapsToDevicePixels="True" />
                </ControlTemplate>
              </Setter.Value>
            </Setter>
          </Style>
        </Setter.Value>
      </Setter>
      <Setter Property="Panel.Background">
        <Setter.Value>
          <SolidColorBrush>#FFDDDDDD</SolidColorBrush>
        </Setter.Value>
      </Setter>
      <Setter Property="Border.BorderBrush">
        <Setter.Value>
          <SolidColorBrush>#FF707070</SolidColorBrush>
        </Setter.Value>
      </Setter>
      <Setter Property="TextElement.Foreground">
        <Setter.Value>
          <DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" />
        </Setter.Value>
      </Setter>
      <Setter Property="Border.BorderThickness">
        <Setter.Value>
          <Thickness>1,1,1,1</Thickness>
        </Setter.Value>
      </Setter>
      <Setter Property="Control.HorizontalContentAlignment">
        <Setter.Value>
          <x:Static Member="HorizontalAlignment.Center" />
        </Setter.Value>
      </Setter>
      <Setter Property="Control.VerticalContentAlignment">
        <Setter.Value>
          <x:Static Member="VerticalAlignment.Center" />
        </Setter.Value>
      </Setter>
      <Setter Property="Control.Padding">
        <Setter.Value>
          <Thickness>1,1,1,1</Thickness>
        </Setter.Value>
      </Setter>
      <Setter Property="Control.Template">
        <Setter.Value>
          <ControlTemplate TargetType="ButtonBase">
            <Border BorderThickness="{TemplateBinding Border.BorderThickness}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="border" SnapsToDevicePixels="True">
              <ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Name="contentPresenter" Margin="{TemplateBinding Control.Padding}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" Focusable="False" />
            </Border>
            <ControlTemplate.Triggers>
              <Trigger Property="Button.IsDefaulted">
                <Setter Property="Border.BorderBrush" TargetName="border">
                  <Setter.Value>
                    <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" />
                  </Setter.Value>
                </Setter>
                <Trigger.Value>
                  <s:Boolean>True</s:Boolean>
                </Trigger.Value>
              </Trigger>
              <Trigger Property="UIElement.IsMouseOver">
                <Setter Property="Panel.Background" TargetName="border">
                  <Setter.Value>
                    <SolidColorBrush>#FFBEE6FD</SolidColorBrush>
                  </Setter.Value>
                </Setter>
                <Setter Property="Border.BorderBrush" TargetName="border">
                  <Setter.Value>
                    <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
                  </Setter.Value>
                </Setter>
                <Trigger.Value>
                  <s:Boolean>True</s:Boolean>
                </Trigger.Value>
              </Trigger>
              <Trigger Property="ButtonBase.IsPressed">
                <Setter Property="Panel.Background" TargetName="border">
                  <Setter.Value>
                    <SolidColorBrush>#FFC4E5F6</SolidColorBrush>
                  </Setter.Value>
                </Setter>
                <Setter Property="Border.BorderBrush" TargetName="border">
                  <Setter.Value>
                    <SolidColorBrush>#FF2C628B</SolidColorBrush>
                  </Setter.Value>
                </Setter>
                <Trigger.Value>
                  <s:Boolean>True</s:Boolean>
                </Trigger.Value>
              </Trigger>
              <Trigger Property="ToggleButton.IsChecked">
                <Setter Property="Panel.Background" TargetName="border">
                  <Setter.Value>
                    <SolidColorBrush>#FFBCDDEE</SolidColorBrush>
                  </Setter.Value>
                </Setter>
                <Setter Property="Border.BorderBrush" TargetName="border">
                  <Setter.Value>
                    <SolidColorBrush>#FF245A83</SolidColorBrush>
                  </Setter.Value>
                </Setter>
                <Trigger.Value>
                  <s:Boolean>True</s:Boolean>
                </Trigger.Value>
              </Trigger>
              <Trigger Property="UIElement.IsEnabled">
                <Setter Property="Panel.Background" TargetName="border">
                  <Setter.Value>
                    <SolidColorBrush>#FFF4F4F4</SolidColorBrush>
                  </Setter.Value>
                </Setter>
                <Setter Property="Border.BorderBrush" TargetName="border">
                  <Setter.Value>
                    <SolidColorBrush>#FFADB2B5</SolidColorBrush>
                  </Setter.Value>
                </Setter>
                <Setter Property="TextElement.Foreground" TargetName="contentPresenter">
                  <Setter.Value>
                    <SolidColorBrush>#FF838383</SolidColorBrush>
                  </Setter.Value>
                </Setter>
                <Trigger.Value>
                  <s:Boolean>False</s:Boolean>
                </Trigger.Value>
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Style.BasedOn>
  <Style.Resources>
    <ResourceDictionary />
  </Style.Resources>
</Style>

That was easy!