ControlTemplate.TargetType property

Applies to Windows and Windows Phone

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

Syntax


public:
property TypeName TargetType { 
   TypeName get();
   void set (TypeName value);
}


<ControlTemplate TargetType="typeName"/>

XAML Values

typeName

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.

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.

Note  

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.

Examples

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


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

        <VisualStateGroup.Transitions>

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

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


Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

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

Metadata

Windows.winmd

See also

ControlTemplate
Style.TargetType
Quickstart: Control templates
ResourceDictionary and XAML resource references

 

 

Show:
© 2014 Microsoft