TargetType property
Collapse the table of content
Expand the table of content

ControlTemplate.TargetType property

Gets or sets the type to which the ControlTemplate is applied.


property TypeName TargetType { 
   TypeName get();
   void set (TypeName value);

<ControlTemplate TargetType="typeName"/>

XAML Values


A string that specifies the type name of the type where the template is applied.

Property value

Type: System.Type [.NET] | TypeName [C++]

The type to which the ControlTemplate is applied. This value is usually set in XAML, not code. See Remarks.


The typical way to specify a TargetType value is through a XAML attribute on the ControlTemplate. When set in XAML, the meaning of TargetType and the values you provide take on some aspects of how types are represented in XAML. In particular, any prefixes that would be necessary to refer to a given type as a XAML element should also be included as the value of the TargetType value string. For example, if a template is intended to target a custom type that must be preceded by the already-mapped prefix "local" in a particular markup scope, then the TargetType value should include that same prefix. This behavior is enabled by built-in conversion behavior in the Windows Runtime XAML parser.

In most cases, you will be setting the TargetType for a ControlTemplate where that ControlTemplate is within a Setter for the Template property. This in turn is within the implicit Style you are defining for a control. The ControlTemplate.TargetType value and the Style.TargetType value of the containing Style should always be the same type reference, referring to the same implicitly named control. Otherwise the control template might not work.

For more info on how to write a control template, see Quickstart: Control templates.


If you have previously programmed XAML for Windows Presentation Foundation (WPF), then you might have used an x:Type markup extension to fill in any XAML values that take a System.Type. The Windows Runtime XAML parser does not support x:Type. Instead, you should refer to the type by name without using any markup extension, and any necessary XAML-to-backing type conversion is already handled by the built-in conversion behavior described in the previous paragraph.


Using TargetType in code

Setting TargetType in code is uncommon. The XAML style/template system is intended to be used by loading XAML as a resource and having all aspects of a control template be ready beforehand. There should be very few scenarios where you would not know info about controls and templates until runtime, where there is a need to set a TargetType on a ControlTemplate created or referenced in code.

Getting a TargetType in code is also uncommon, but you might do this to double-check that a ControlTemplate that you've loaded from XAML is compatible with a given control target that you got from the runtime tree.

Tip  If you are programming using a Microsoft .NET language (C# or Microsoft Visual Basic), the TypeName type projects as System.Type. When programming using C#C#, you usually use the typeof operator to get references to the System.Type of a type based on a type name as a string. In Visual Basic, use GetType.


The following example sets the TargetType property to Button to create a simple ControlTemplate for a Button.

<ControlTemplate TargetType="Button">
  <Grid >
      <VisualStateGroup x:Name="CommonStates">


          <!--Take one half second to transition to the PointerOver state.-->
          <VisualTransition To="PointerOver" 
        <VisualState x:Name="Normal" />

        <!--Change the SolidColorBrush, ButtonBrush, to red when the
            Pointer is over the button.-->
        <VisualState x:Name="PointerOver">
            <ColorAnimation Storyboard.TargetName="ButtonBrush" 
                            Storyboard.TargetProperty="Color" To="Red" />
      <SolidColorBrush x:Name="ButtonBrush" Color="Green"/>

Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0


Windows::UI::Xaml::Controls [C++]



Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]


Windows::UI::Xaml::Controls [C++]



See also

Quickstart: Control templates
ResourceDictionary and XAML resource references



© 2016 Microsoft