DataTemplate Class
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Describes the visual structure of a data object.
System.Windows::DependencyObject
System.Windows::FrameworkTemplate
System.Windows::DataTemplate
Assembly: System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.
The DataTemplate type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Dispatcher | Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.) |
| Name | Description | |
|---|---|---|
![]() | CheckAccess | Determines whether the calling thread has access to this object. (Inherited from DependencyObject.) |
![]() | ClearValue | Clears the local value of a dependency property. (Inherited from DependencyObject.) |
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetAnimationBaseValue | Returns any base value established for a Windows Phone dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | GetValue | Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.) |
![]() | LoadContent | Creates the UIElement objects in the DataTemplate. |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ReadLocalValue | Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.) |
![]() | SetValue | Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
You typically use a DataTemplate to specify the visual representation of your data. DataTemplate objects are particularly useful when you are binding an ItemsControl such as a ListBox to an entire collection. Without specific instructions, a ListBox displays the string representation of the objects in a collection. In that case, you can use a DataTemplate to define the appearance of your data objects. The content of your DataTemplate becomes the visual structure of your data objects.
You can use data binding in a DataTemplate. For example, suppose that a ListBox is bound to a collection of Customer objects and has the ItemTemplate property set to a DataTemplate. When the ListBox is created, a ListBoxItem is created for each Customer in the collection, and the DataContext of the ListBoxItem is set to the appropriate customer. In other words, the DataContext of the first ListBoxItem is set to the first customer, the DataContext of the second ListBoxItem is set to the second customer, and so on. You can bind elements in the DataTemplate to properties of the Customer object.
You can also use a DataTemplate to share UIElement objects across multiple ContentControl objects. For example, suppose you need multiple buttons on your application to have the same graphic. You can create a DataTemplate that contains the graphic and use it as the ContentTemplate for the buttons. For more information, see ContentControl::ContentTemplate.
You can place a DataTemplate as the direct child of an object.ItemTemplate property element. You can also define a DataTemplate as a resource and then reference the resource as the value of the ItemTemplate property.
The XAML usage that defines the content for creating a data template is not exposed as a settable property. It is special behavior built into the XAML processing of a DataTemplate object element.
The following example uses a DataTemplate to display the items of a ListBox. In this example, the ListBox is bound to a collection of Customer objects. The DataTemplate contains TextBlock controls that bind to the FirstName, LastName, and Address properties. For more information on data binding, see Data binding for Windows Phone 8.
<Grid> <Grid.Resources> <src:Customers x:Key="customers"/> </Grid.Resources> <ListBox ItemsSource="{StaticResource customers}" Margin="0,5,0,10"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" > <TextBlock Padding="3,0,3,0" Text="{Binding FirstName}" FontSize="{StaticResource PhoneFontSizeSmall}"/> <TextBlock Text="{Binding LastName}" FontSize="{StaticResource PhoneFontSizeSmall}"/> <TextBlock Text=", " FontSize="{StaticResource PhoneFontSizeSmall}"/> <TextBlock Text="{Binding Address}" FontSize="{StaticResource PhoneFontSizeSmall}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid>
The following example shows the Customer class and the collection that the ListBox is bound to.


