ItemsControl.ItemTemplate 属性

2013/12/13

获取或设置用于显示每个项的 DataTemplate

Namespace:  System.Windows.Controls
程序集:  System.Windows(位于 System.Windows.dll 中)
XMLNS for XAML: 未映射到 xmlns。

声明
Public Property ItemTemplate As DataTemplate
<itemsControl>
  <itemsControl.ItemTemplate>
    singleDataTemplate
  </itemsControl.ItemTemplate>
</itemsControl>
<itemsControl ItemTemplate="resourceReferenceToDataTemplate"/>

XAML 值

singleDataTemplate

一个 DataTemplate 对象元素。DataTemplate 通常具有多个子元素,它们定义数据表示形式的可视外观。

resourceReferenceToDataTemplate

从资源集合对现有 DataTemplate 进行的资源引用。资源引用必须通过键来指定所需的 DataTemplate

属性值

类型: System.Windows.DataTemplate
指定数据对象的可视化的模板。默认值为 Nothing

依赖项属性标识符字段: ItemTemplateProperty

您使用 ItemTemplate 属性来指定数据的直观表示形式。ItemTemplate 对于将 ItemsSource 绑定到数据特别有帮助。同时使用 DataTemplate 来定义数据对象的外观。DataTemplate 的内容变成数据对象的可视结构。如果不设置 ItemTemplate,则 ItemsControl 将显示集合中对象的字符串表示形式。

下面的示例使用 DataTemplate 来显示继承自 ItemsControlListBox 的项。在此示例中,ListBox 绑定到 Customer 对象的集合。DataTemplate 包含 TextBlock 控件,这些控件绑定到 FirstNameLastNameAddress 属性。有关数据绑定的更多信息,请参见Windows Phone 的数据绑定


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


下面的示例演示 Customer 类和 ListBox 绑定到的集合。


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


Windows Phone OS

受以下版本支持: 8.0, 7.1, 7.0

Windows Phone

显示:
© 2015 Microsoft