WPF: The Simplest Way to Get the Default Template of a Control as XAML

Every WPF Developer has probably faced this issue before. You want to change the default template of a control and need it 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 tool
  3. Use Expression Blend

In my opinion, none of these solutions is 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:

1 public static void SaveDefaultTemplate() 2 { 3 var control = Application.Current.FindResource(typeof(ProgressBar)); 4 using (XmlTextWriter writer = new XmlTextWriter(@"defaultTemplate.xml", System.Text.Encoding.UTF8)) 5 { 6 writer.Formatting = Formatting.Indented; 7 XamlWriter.Save(control, writer); 8 } 9 }

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

1 <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"> 2 <Style.BasedOn> 3 <Style TargetType="ButtonBase"> 4 <Style.Resources> 5 <ResourceDictionary /> 6 </Style.Resources> 7 <Setter Property="FrameworkElement.FocusVisualStyle"> 8 <Setter.Value> 9 <Style TargetType="IFrameworkInputElement"> 10 <Style.Resources> 11 <ResourceDictionary /> 12 </Style.Resources> 13 <Setter Property="Control.Template"> 14 <Setter.Value> 15 <ControlTemplate> 16 <Rectangle Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2" Margin="2,2,2,2" SnapsToDevicePixels="True" /> 17 </ControlTemplate> 18 </Setter.Value> 19 </Setter> 20 </Style> 21 </Setter.Value> 22 </Setter> 23 <Setter Property="Panel.Background"> 24 <Setter.Value> 25 <SolidColorBrush>#FFDDDDDD</SolidColorBrush> 26 </Setter.Value> 27 </Setter> 28 <Setter Property="Border.BorderBrush"> 29 <Setter.Value> 30 <SolidColorBrush>#FF707070</SolidColorBrush> 31 </Setter.Value> 32 </Setter> 33 <Setter Property="TextElement.Foreground"> 34 <Setter.Value> 35 <DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" /> 36 </Setter.Value> 37 </Setter> 38 <Setter Property="Border.BorderThickness"> 39 <Setter.Value> 40 <Thickness>1,1,1,1</Thickness> 41 </Setter.Value> 42 </Setter> 43 <Setter Property="Control.HorizontalContentAlignment"> 44 <Setter.Value> 45 <x:Static Member="HorizontalAlignment.Center" /> 46 </Setter.Value> 47 </Setter> 48 <Setter Property="Control.VerticalContentAlignment"> 49 <Setter.Value> 50 <x:Static Member="VerticalAlignment.Center" /> 51 </Setter.Value> 52 </Setter> 53 <Setter Property="Control.Padding"> 54 <Setter.Value> 55 <Thickness>1,1,1,1</Thickness> 56 </Setter.Value> 57 </Setter> 58 <Setter Property="Control.Template"> 59 <Setter.Value> 60 <ControlTemplate TargetType="ButtonBase"> 61 <Border BorderThickness="{TemplateBinding Border.BorderThickness}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="border" SnapsToDevicePixels="True"> 62 <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" /> 63 </Border> 64 <ControlTemplate.Triggers> 65 <Trigger Property="Button.IsDefaulted"> 66 <Setter Property="Border.BorderBrush" TargetName="border"> 67 <Setter.Value> 68 <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" /> 69 </Setter.Value> 70 </Setter> 71 <Trigger.Value> 72 <s:Boolean>True</s:Boolean> 73 </Trigger.Value> 74 </Trigger> 75 <Trigger Property="UIElement.IsMouseOver"> 76 <Setter Property="Panel.Background" TargetName="border"> 77 <Setter.Value> 78 <SolidColorBrush>#FFBEE6FD</SolidColorBrush> 79 </Setter.Value> 80 </Setter> 81 <Setter Property="Border.BorderBrush" TargetName="border"> 82 <Setter.Value> 83 <SolidColorBrush>#FF3C7FB1</SolidColorBrush> 84 </Setter.Value> 85 </Setter> 86 <Trigger.Value> 87 <s:Boolean>True</s:Boolean> 88 </Trigger.Value> 89 </Trigger> 90 <Trigger Property="ButtonBase.IsPressed"> 91 <Setter Property="Panel.Background" TargetName="border"> 92 <Setter.Value> 93 <SolidColorBrush>#FFC4E5F6</SolidColorBrush> 94 </Setter.Value> 95 </Setter> 96 <Setter Property="Border.BorderBrush" TargetName="border"> 97 <Setter.Value> 98 <SolidColorBrush>#FF2C628B</SolidColorBrush> 99 </Setter.Value> 100 </Setter> 101 <Trigger.Value> 102 <s:Boolean>True</s:Boolean> 103 </Trigger.Value> 104 </Trigger> 105 <Trigger Property="ToggleButton.IsChecked"> 106 <Setter Property="Panel.Background" TargetName="border"> 107 <Setter.Value> 108 <SolidColorBrush>#FFBCDDEE</SolidColorBrush> 109 </Setter.Value> 110 </Setter> 111 <Setter Property="Border.BorderBrush" TargetName="border"> 112 <Setter.Value> 113 <SolidColorBrush>#FF245A83</SolidColorBrush> 114 </Setter.Value> 115 </Setter> 116 <Trigger.Value> 117 <s:Boolean>True</s:Boolean> 118 </Trigger.Value> 119 </Trigger> 120 <Trigger Property="UIElement.IsEnabled"> 121 <Setter Property="Panel.Background" TargetName="border"> 122 <Setter.Value> 123 <SolidColorBrush>#FFF4F4F4</SolidColorBrush> 124 </Setter.Value> 125 </Setter> 126 <Setter Property="Border.BorderBrush" TargetName="border"> 127 <Setter.Value> 128 <SolidColorBrush>#FFADB2B5</SolidColorBrush> 129 </Setter.Value> 130 </Setter> 131 <Setter Property="TextElement.Foreground" TargetName="contentPresenter"> 132 <Setter.Value> 133 <SolidColorBrush>#FF838383</SolidColorBrush> 134 </Setter.Value> 135 </Setter> 136 <Trigger.Value> 137 <s:Boolean>False</s:Boolean> 138 </Trigger.Value> 139 </Trigger> 140 </ControlTemplate.Triggers> 141 </ControlTemplate> 142 </Setter.Value> 143 </Setter> 144 </Style> 145 </Style.BasedOn> 146 <Style.Resources> 147 <ResourceDictionary /> 148 </Style.Resources> 149 </Style>

Category:Business
PREVIOUS POST
AzureAdventures: Setting up the Adventureworks Database in under 10 Minutes
NEXT POST
Microsoft TechTalk: S.O.L.I.D. Principles and DDD

0 Comment

LEAVE A REPLY

This site uses Akismet to reduce spam. Learn how your comment data is processed.

15 49.0138 8.38624 1 0 4000 1 https://manuelmeyer.net 300 0