71

I'm wondering how I can extract (get a copy) of the Default Template of a given control using Visual Studio. I know this can be done with Expression Blend (right click a control, "Edit Template" -> "Edit a Copy...") which then copies the default control template in my Xaml. But can this be done with Visual Studio at all?

Shai UI
  • 50,568
  • 73
  • 204
  • 309

6 Answers6

107

2015 update with clear instructions

In Visual Studio 2013, you can get the default ControlTemplate of a control in a few simple steps.

  1. In the WPF designer, select the relevant control, or place the mouse cursor on the relevant control in the XAML.

  2. Press F4 to open the Properties Window.

  3. Open the Miscellaneous category to find the Template property, or type Template in the search field at the top of the Window.

  4. Click on the little square to the right of the Template field and select the Convert to New Resource... option:

enter image description here

  1. In the popup dialog, name the new ControlTemplate to be added and decide where you want it to be defined:

enter image description here

  1. Click on the OK button.

EDIT >>>

In Visual Studio 2019 and later, this option seems to be disabled for some reason. A workaround can be found by right-clicking the control in the design view and selecting "Edit Template", then selecting "Edit a Copy...".

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Thanks! This really helped me out when I needed to get the control template for the DatePickerTextBox so I could remove the annoying inner border. – Matt Becker May 08 '15 at 14:44
  • This is really great - thanks! Do you know if there is a way to similarly extract the full Style? The 'Convert to New Resource' command is disabled on the Style property. – Andrew Hanlon Nov 24 '15 at 14:03
  • @AndrewHanlon, were you running the solution at that point? That's the only reason that I can think of that would result in the Convert to New Resource button being disabled. – Sheridan Nov 24 '15 at 14:43
  • The control disappears, because VS inserts an empty template: – Soonts Apr 11 '16 at 21:00
  • 1
    These instructions stand correct for VS2017 as well. – itsho Sep 12 '17 at 14:44
  • This still works in VS2022, though the little square is now a cylinder with a drop-down arrow. – fadden Apr 09 '23 at 16:01
35

From Visual studio - right click the control, choose properties,

In the properties window look for the Template Property and right click it, choose Extract Value To Resource

That will create a copy of the template in the XAML for you to work on.

Royi Mindel
  • 1,258
  • 12
  • 35
  • 1
    When I did this on a Textbox the Template property was disabled so I couldn't right-click on it. – Jeff Kotula Apr 03 '14 at 14:09
  • 3
    @Jeff: Rightclick your element in the designer window (not in the Xaml-Code), then "Edit Template..." and "Edit a Copy..." – Pollitzer Nov 15 '15 at 11:18
9

Just to update this question, in VS 11 the XAML designer allows you to do this just like Expression Blend.

erodewald
  • 1,815
  • 20
  • 45
5

One thing to keep in mind: if you already have a style defined somewhere that targets the given control then all of the above described options will be disabled. I had the following bit of code in my App.xaml file:

<Application.Resources>
    <Style TargetType="Button">
        <Setter Property="IsTabStop" Value="False"/>
    </Style>
</Application.Resources>

I was pulling my hair out trying to figure out why the edit a copy... and convert to new resource... options described in the other answers were disabled for my Button (which was defined in a completely different file). I commented out the above style code for Button and suddenly both options weren't disabled anymore.

Moral of the story: VS won't generate a style containing a template copy for you if a style already exists for that element anywhere in your code.

Kirk Kuykendall
  • 822
  • 1
  • 12
  • 26
2

In VS19 I wasn't able to do this through properties.

However, I was able to right click the control in design mode Edit Template and Edit a Copy.

Tom Huntington
  • 2,260
  • 10
  • 20
1

As far as I know it's not possible. However, you can use Show Me The Template to view the default template for a given control.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
  • 9
    I used to use Show Me The Template but found that the templates would occasionally be missing some functionality, so have switched to using the templates found on [MSDN](http://msdn.microsoft.com/en-us/library/aa970773.aspx) – Rachel Jan 11 '12 at 21:20
  • Link is now broken – Drake Oct 24 '18 at 09:26