ItemsControl.ItemTemplate Property

July 28, 2014

Gets or sets the DataTemplate used to display each item.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

public DataTemplate ItemTemplate { get; set; }
<itemsControl>
  <itemsControl.ItemTemplate>
    singleDataTemplate
  </itemsControl.ItemTemplate>
</itemsControl>
<itemsControl ItemTemplate="resourceReferenceToDataTemplate"/>

XAML Values

singleDataTemplate

A single DataTemplate object element. That DataTemplate would typically have multiple child elements that define the visual appearance of the data representation.

resourceReferenceToDataTemplate

A resource reference to an existing DataTemplate from a resources collection. The resource reference must specify the desired DataTemplate by key.

Property Value

Type: System.Windows.DataTemplate
The template that specifies the visualization of the data objects. The default is null.

Dependency property identifier field: ItemTemplateProperty

You use the ItemTemplate property to specify the visual representation of your data. ItemTemplate is particularly useful when you bind the ItemsSource to data. You use a DataTemplate to define the appearance of your data objects. The content of your DataTemplate becomes the visual structure of your data objects. If you do not set the ItemTemplate, the ItemsControl displays the string representation of the objects in a collection.

The following example uses a DataTemplate to display the items of a ListBox, which inherits from ItemsControl. 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 about 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.


public class Customer
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String Address { get; set; }

    public Customer(String firstName, String lastName, String address)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
        this.Address = address;
    }

}

public class Customers : ObservableCollection<Customer>
{
    public Customers()
    {
        Add(new Customer("Michael", "Anderberg",
                "12 North Third Street, Apartment 45"));
        Add(new Customer("Chris", "Ashton",
                "34 West Fifth Street, Apartment 67"));
        Add(new Customer("Cassie", "Hicks",
                "56 East Seventh Street, Apartment 89"));
        Add(new Customer("Guido", "Pica",
                "78 South Ninth Street, Apartment 10"));
    }

}


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft