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?
A ControlTemplate will generally only contain TemplateBinding expressions, binding back to the properties on the control itself, while a DataTemplate will contain standard Binding expressions, binding to the properties of its DataContext (the business/domain object or view model).
The ControlTemplate allows you to specify the visual structure of a control. The control author can define the default ControlTemplate and the application author can override the ControlTemplate to reconstruct the visual structure of the control.
The ControlTemplate contains the tree of elements that define the desired look. After you define a ControlTemplate you can attach it to any Control or Page by setting it's TemplateProperty. In this example I am also showing you how to use Triggers with ControlTemplate.
2015 update with clear instructions
In Visual Studio 2013, you can get the default ControlTemplate
of a control in a few simple steps.
In the WPF designer, select the relevant control, or place the mouse cursor on the relevant control in the XAML.
Press F4 to open the Properties Window.
Open the Miscellaneous category to find the Template property, or type Template in the search field at the top of the Window.
Click on the little square to the right of the Template field and select the Convert to New Resource... option:
ControlTemplate
to be added and decide where you want it to be defined: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...".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With