Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ControlTemplate for existing controls in WPF

People also ask

How do I customize WPF controls?

Create a new WPF project and then right-click on your solution and select Add > New Item... It will open the following window. Now select Custom Control (WPF) and name it MyCustomControl. Click the Add button and you will see that two new files (Themes/Generic.

What is difference between a ControlTemplate and a DataTemplate in WPF?

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


Check out StyleSnooper:

enter image description here

It will dump out the standard styles (and therefore templates too) for the built in controls. You can also load in a specific DLL that contains WPF controls and view the default styles for those too.


The styles along with template examples are up on MSDN for download, see the Default WPF Themes link.

However you can also extend the existing style without redefining everything by using the BasedOn attribute.


If you have Expression Blend you can:

  1. Drag the control onto the design surface
  2. Right click the control and choose Edit Template -> Edit Copy

When you do this, Blend will extract the base template from the control and explicitly declare it within document/application as a resource which you can then edit to your liking. You can do this for any control.


The book "Pro WPF in C# 2008", by Matthew MacDonald, includes a Control Template browser in Chapter 15. I believe you can simply download the sample code from the Apress web site.


2020 Update:

All the styles and templates of WPF components are now moved here.
But, before creating a new component, just check this to see if you have a good alternative to that (maybe changing the style can work).
Note: Usually, it has a bunch of DynamicResource bindings that you might want to replace with yours and make static. If you don't want to do much manual work, you might consider using the second solution below.

The second and shorter solution might be using Microsoft Blend to extract the template with the following steps:

Right-click on the control > Edit Template> Edit Current OR Edit a Copy

But be careful with this one, as it doesn't always export the whole template,
but the necessary part of it.
Just consider comparing this template with the official one (mentioned above) to make sure everything is fine.