This documentation is archived and is not being maintained.

DataTemplate::DataType Property

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

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

property Object^ DataType {
	Object^ get ();
	void set (Object^ value);
    <Object .../>
<object DataType="typeName"/>

XAML Values


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.

Property Value

Type: System::Object
The default value is nullptr.

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">

The following example shows how to display multiple collections and items as one list using the CompositeCollection class. In this example, GreekGods is an ObservableCollection<T> of GreekGod custom objects. Data templates are defined so that GreekGod objects and GreekHero objects appear with a gold and a cyan foreground color respectively.

<Window Background="Cornsilk"
    <c:GreekGods x:Key="GreekGodsData"/>

    <XmlDataProvider x:Key="GreekHeroesData" XPath="GreekHeroes/Hero">
      <GreekHeroes xmlns="">
        <Hero Name="Jason" />
        <Hero Name="Hercules" />
        <Hero Name="Bellerophon" />
        <Hero Name="Theseus" />
        <Hero Name="Odysseus" />
        <Hero Name="Perseus" />

    <DataTemplate DataType="{x:Type c:GreekGod}">
      <TextBlock Text="{Binding Path=Name}" Foreground="Gold"/>
    <DataTemplate DataType="Hero">
      <TextBlock Text="{Binding XPath=@Name}" Foreground="Cyan"/>
    <TextBlock FontSize="18" FontWeight="Bold" Margin="10"
      HorizontalAlignment="Center">Composite Collections Sample</TextBlock>
    <ListBox Name="myListBox" Height="300" Width="200" Background="White">
            Collection="{Binding Source={StaticResource GreekGodsData}}" />
            Collection="{Binding Source={StaticResource GreekHeroesData}}" />
          <ListBoxItem Foreground="Red">Other Listbox Item 1</ListBoxItem>
          <ListBoxItem Foreground="Red">Other Listbox Item 2</ListBoxItem>


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.