How to Extract Default Control Template In Visual Studio?

WpfVisual StudioXaml

Wpf Problem Overview


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?

Wpf Solutions


Solution 1 - Wpf

>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...".

Solution 2 - Wpf

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.

Solution 3 - Wpf

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

Solution 4 - Wpf

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.

Solution 5 - Wpf

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.

Solution 6 - Wpf

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShai UIView Question on Stackoverflow
Solution 1 - WpfSheridanView Answer on Stackoverflow
Solution 2 - WpfRoyi MindelView Answer on Stackoverflow
Solution 3 - WpferodewaldView Answer on Stackoverflow
Solution 4 - WpfJohn Christopher LinstrumView Answer on Stackoverflow
Solution 5 - WpfTom HuntingtonView Answer on Stackoverflow
Solution 6 - WpfAdi LesterView Answer on Stackoverflow