DataTemplate.DataType Property

Definition

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

public:
 property System::Object ^ DataType { System::Object ^ get(); void set(System::Object ^ value); };
public object DataType { get; set; }
[System.Windows.Markup.Ambient]
public object DataType { get; set; }
member this.DataType : obj with get, set
[<System.Windows.Markup.Ambient>]
member this.DataType : obj with get, set
Public Property DataType As Object

Property Value

The default value is null.

Attributes

Remarks

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

XAML Attribute Usage

<object DataType="typeName"/>  

XAML Values

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.

Applies to

See also