DataType Property
Collapse the table of content
Expand the table of content

DataTemplate.DataType Property


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

Namespace:   System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

public object DataType { get; set; }

Property Value

Type: System.Object

The default value is null.

This property that is very similar to the TargetType property of the Style class. When you set this property to the data type without specifying an x:Key, the DataTemplate gets applied automatically to data objects of that type. Note that when you do that the x:Key is set implicitly. Therefore, if you assign this DataTemplate an x:Key value, you are overriding the implicit x:Key and the DataTemplate would not be applied automatically.

Also note that if you are binding a ContentControl to a collection of Task objects, the ContentControl does not use the DataTemplate automatically. This is because the binding on a ContentControl needs more information to distinguish whether you want to bind to an entire collection or the individual objects. If your ContentControl is tracking the selection of an ItemsControl type, you can set the Path property of the ContentControl binding to "/" to indicate that you are interested in the current item. For an example, see How to: Bind to a Collection and Display Information Based on Selection. Otherwise, you need to specify the DataTemplate explicitly by setting the ContentTemplate property.

The DataType property is particularly useful when you have a CompositeCollection of different types of data objects.

If this property targets an XML element that is not in the default namespace, you must precede the element name with the namespace or a namespace designator. For XML exposed through LINQ for XML, the namespace appears in braces, preceded by a brace escape sequence:

<DataTemplate DataType="{}{http://myNamespace}Details">

For XML exposed through XPath, the element name is preceded by a namespace designator established by an instance of XmlNamespaceMapping:

<DataTemplate DataType="mn:Details">

<object DataType="typeName"/>


If the template is intended for object data, this property contains the type name of the data object (as a string). To refer to the type name of the class, use the x:Type Markup Extension. If the template is intended for XML data, this property contains the XML element name. See the documentation remarks for details about specifying a non-default namespace for the XML element.

Legacy Code Example

Code snippet is not found. Confirm that the code snippet name 'HOWDOI' is correct.

.NET Framework
Available since 3.0
Available since 5.0
Return to top
© 2015 Microsoft