ControlTemplate.TargetType Property

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

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

public Type TargetType { get; set; }
/** @property */
public Type get_TargetType ()

/** @property */
public void set_TargetType (Type value)

public function get TargetType () : Type

public function set TargetType (value : Type)

<object TargetType="typeName"/>
 typeName  The type name of the class. To refer to the Type name of the class, use the XAML-defined markup extension.  

Property Value

The default value is a null reference (Nothing in Visual Basic).

Exception typeCondition


The TargetType property must not be a null reference (Nothing in Visual Basic) if the definition of the template has a ContentPresenter.


The specified types is not valid. The TargetType of a ControlTemplate must be or inherit from a Control, a Page, or a PageFunctionBase.

If you have a standalone ControlTemplate in the resources section with the TargetType property set to a type, the ControlTemplate does not get applied to that type automatically. Instead, you need to specify an x:Key and apply the template explicitly.

Also note that the TargetType property is required on a ControlTemplate if the template definition contains a ContentPresenter.

The following example demonstrates the use of this property:

<Style x:Key="{x:Type Label}" TargetType="Label">
  <Setter Property="HorizontalContentAlignment" Value="Left"/>
  <Setter Property="VerticalContentAlignment" Value="Top"/>
  <Setter Property="Template">
      <ControlTemplate TargetType="Label">
            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
          <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground"
                    Value="{StaticResource DisabledForegroundBrush}"/>

The above example uses the following resource:

<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#888" />

For the complete sample, see Styling with ControlTemplates Sample.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions