Windows Dev Center

The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ControlTemplate Class


Specifies the visual structure and behavioral aspects of a Control that can be shared across multiple instances of the control.

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

[<LocalizabilityAttribute(LocalizationCategory.None, Readability = Readability.Unreadable)>]
type ControlTemplate = 
        inherit FrameworkTemplate


Initializes a new instance of the ControlTemplate class.


Initializes a new instance of the ControlTemplate class with the specified target type.


Gets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)


Gets a value that indicates whether this template has optimized content.(Inherited from FrameworkTemplate.)


Gets a value that indicates whether this object is in an immutable state so it cannot be changed.(Inherited from FrameworkTemplate.)


Gets or sets the collection of resources that can be used within the scope of this template.(Inherited from FrameworkTemplate.)


Gets or sets the type for which this ControlTemplate is intended.


Gets or sets a reference to the object that records or plays the XAML nodes for the template when the template is defined or applied by a writer.(Inherited from FrameworkTemplate.)


Gets a collection of TriggerBase objects that apply property changes or perform actions based on specified conditions.


Gets or sets the root node of the template.(Inherited from FrameworkTemplate.)


Determines whether the calling thread has access to this DispatcherObject.(Inherited from DispatcherObject.)


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodFindName(String, FrameworkElement)

Finds the element associated with the specified name defined within this template.(Inherited from FrameworkTemplate.)


Serves as the default hash function. (Inherited from Object.)


Gets the Type of the current instance.(Inherited from Object.)


Loads the content of the template as an instance of an object and returns the root element of the content.(Inherited from FrameworkTemplate.)


Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodRegisterName(String, Object)

Registers a new name/object pair into the current name scope.(Inherited from FrameworkTemplate.)


Locks the template so it cannot be changed.(Inherited from FrameworkTemplate.)


Returns a value that indicates whether serialization processes should serialize the value of the Resources property on instances of this class.(Inherited from FrameworkTemplate.)


Returns a value that indicates whether serialization processes should serialize the value of the VisualTree property on instances of this class.(Inherited from FrameworkTemplate.)


Returns a string that represents the current object.(Inherited from Object.)


Removes a name/object mapping from the XAML namescope.(Inherited from FrameworkTemplate.)


Checks the templated parent against a set of rules.(Overrides FrameworkTemplate.ValidateTemplatedParent(FrameworkElement).)


Enforces that the calling thread has access to this DispatcherObject.(Inherited from DispatcherObject.)


This API supports the product infrastructure and is not intended to be used directly from your code. Returns an object that has the provided identifying name. (Inherited from FrameworkTemplate.)


Queries whether a specified ambient property is available in the current scope.(Inherited from FrameworkTemplate.)

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.

Control templating is one of the many features offered by the WPF styling and templating model. The styling and templating model provides you with such great flexibility that in many cases you do not need to write your own controls. If you are an application author that wants to change the visualization of your control or to replace the ControlTemplate of an existing control, see the Styling and Templating topic for examples and an in-depth discussion.

If you are writing your own control, see "Create a Custom Control" in the Control Authoring Overview.

A ControlTemplate is intended to be a self-contained unit of implementation detail that is invisible to outside users and objects, including styles. The only way to manipulate the content of the control template is from within the same control template.



Object element for ControlTemplate or a derived class.


A single XAML element as the immediate child of the ControlTemplate (or a derived class). Templates must have a single root node. In order to generate a useful template, the element chosen as VisualTreeRootNode is expected to support a content model of its own, often a model that supports multiple child elements.


One or more elements that complete the intended template. If the element chosen as VisualTreeRootNode only supports a single child, then there can only be one element declared as VisualTreeNodeContents. It is also possible (though uncommon) to provide text content if the chosen VisualTreeRootNode supports a text content property.

The following shows a Button Style that sets the ControlTemplate of a Button:

When this gets applied, the Button appears as an Ellipse:

Button ControlTemplate sample

When you set the Template property of a Control to a new ControlTemplate as in the above example, you are replacing the entire template. What the Button looks like when it is in focus or pressed is all part of the default appearance of the button that you are replacing. Therefore, depending on your needs, you may want to put in your definition what your button should look like when it is pressed, and so on, as in the following example:

Note that this example references resources that are not shown here. For the complete sample, see Styling with ControlTemplates Sample. That sample provides examples of control templates for many controls and is the best way for you to get started with creating control templates.

.NET Framework
Available since 3.0
Available since 2.0
Windows Phone Silverlight
Available since 7.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
© 2015 Microsoft