Udostępnij za pośrednictwem


ControlTemplate.TargetType Property

Definition

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

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

void TargetType(TypeName value);
public System.Type TargetType { get; set; }
var typeName = controlTemplate.targetType;
controlTemplate.targetType = typeName;
Public Property TargetType As Type
<ControlTemplate TargetType="typeName"/>

Property Value

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#, 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 Microsoft Visual Basic, use GetType.

Applies to

See also