Share via


x:Type Markup Extension

Supplies the Type object for the provided type.

XAML Attribute Usage

<object property="{x:Type prefix:typeNameValue}" .../>

XAML Object Element Usage

<object>
  <object.property>
    <x:Type TypeName="prefix:typeNameValue"/>
  </object.property>
</object>

XAML Values

prefix

Optional. A prefix that maps a non-default XML namespace. Specifying a prefix is frequently not necessary, see Remarks.

typeNameValue

Required. A type name resolvable to the current default XML namespace, or the specified mapped prefix if prefix is supplied.

Remarks

x:Type is used to supply the attribute value for a property that takes Type. However, many properties that take Type as a value are able to accept the name of the type directly (the string value of the type's Name); check the documentation for the specific property for details. x:Type is essentially a markup extension equivalent for a typeof() operator in C# or the GetType operator in Microsoft Visual Basic .NET.

You define the default XML namespace for any given XAML  page as an attribute on the root element. Generally, the default XML namespace you use for Windows Presentation Foundation (WPF) programming is the WPF namespace. The identifier for that namespace is https://schemas.microsoft.com/winfx/2006/xaml/presentation. The vast majority of types that are intended for common WPF application programming are within this namespace. Therefore you generally do not need to map a prefix to obtain a type when using x:Type. You may need to map a prefix if you are referencing a type from a custom assembly, or for types that exist in a WPF assembly but are within a CLR namespace that was not mapped to be part of the WPF namespace from that assembly. For information on prefixes, XML namespaces, and mapping CLR namespaces, see XAML Namespaces and Namespace Mapping.

Attribute syntax is the most common syntax used with this markup extension. The string token provided after the x:Type identifier string is assigned as the TypeName value of the underlying TypeExtension extension class. The value of this attribute is the Name of the desired type.

x:Type can be used in object element syntax. In this case, specifying the value of the TypeName property is required to properly initialize the extension.

x:Type can also be used in a verbose attribute usage that specifies the TypeName property as a property=value pair:

<object property="{x:Type TypeName=typeNameValue}" .../>

The verbose usage is often useful for extensions that have more than one settable property, or if some properties are optional. Because x:Type has only one settable property, which is required, this verbose usage is not typical.

In the WPF XAML processor implementation, the handling for this markup extension is defined by the TypeExtension class.

x:Type is a markup extension. Markup extensions are typically implemented when there is a requirement to escape attribute values to be other than literal values or handler names, and the requirement is more global than just putting type converters on certain types or properties. All markup extensions in XAML use the { and } characters in their attribute syntax, which is the convention by which a XAML processor recognizes that a markup extension must process the attribute. For more information, see Markup Extensions and XAML.

See Also

Concepts

Styling and Templating

XAML Overview

Markup Extensions and XAML

Reference

Style