Поделиться через


DataTemplate Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Describes the visual structure of a data object.

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    System.Windows.FrameworkTemplate
      System.Windows.DataTemplate
        System.Windows.Controls.Pivot.PivotViewerItemTemplate
        System.Windows.HierarchicalDataTemplate

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

Syntax

'Declaration
Public Class DataTemplate _
    Inherits FrameworkTemplate
public class DataTemplate : FrameworkTemplate
<DataTemplate ...>
  templateContent
</DataTemplate>

XAML Values

  • templateContent
    The tree of objects that defines this DataTemplate. The tree must have a single root element, and that root element can have zero or more child elements.

The DataTemplate type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows Phone DataTemplate() Initializes a new instance of the DataTemplate class without initializing the DataType property.
Public method DataTemplate(Object) Initializes a new instance of the DataTemplate class, setting the DataType property to the specified value.

Top

Properties

  Name Description
Public property DataType Gets or sets the type for which this DataTemplate is intended.
Public propertySupported by Silverlight for Windows Phone Dispatcher Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone CheckAccess Determines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone 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.)
Public methodSupported by Silverlight for Windows Phone GetAnimationBaseValue Returns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone LoadContent Creates the UIElement objects in the DataTemplate.
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

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.

Examples

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.

<Grid>
    <Grid.Resources>
        <src:Customers x:Key="customers"/>
    </Grid.Resources>

    <ListBox ItemsSource="{StaticResource customers}" Width="350" Margin="0,5,0,10">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Padding="5,0,5,0"
          Text="{Binding FirstName}" />
                    <TextBlock Text="{Binding LastName}" />
                    <TextBlock Text=", " />
                    <TextBlock Text="{Binding Address}" />
                </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
    Private _firstName As String
    Private _lastName As String
    Private _address As String

    Public Property FirstName() As String
        Get
            Return _firstName
        End Get

        Set(ByVal value As String)
            _firstName = value
        End Set
    End Property

    Public Property LastName() As String
        Get
            Return _lastName
        End Get

        Set(ByVal value As String)
            _lastName = value
        End Set
    End Property

    Public Property Address() As String
        Get
            Return _address
        End Get

        Set(ByVal value As String)
            _address = value
        End Set
    End Property

    Public Sub New(ByVal firstName As String, ByVal lastName As String, ByVal address As String)
        Me.FirstName = firstName
        Me.LastName = lastName
        Me.Address = address
    End Sub

End Class

Public Class Customers
    Inherits ObservableCollection(Of Customer)

    Public Sub New()
        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"))
    End Sub

End Class
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"));
    }

}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.