DataTemplate 类

定义

描述数据对象的可视结构。Describes the visual structure of a data object.

public ref class DataTemplate : System::Windows::FrameworkTemplate
[System.Windows.Markup.DictionaryKeyProperty("DataTemplateKey")]
public class DataTemplate : System.Windows.FrameworkTemplate
type DataTemplate = class
    inherit FrameworkTemplate
Public Class DataTemplate
Inherits FrameworkTemplate
继承
派生
属性

示例

下面的示例演示如何创建 DataTemplate 内联。The following example shows how to create a DataTemplate inline. DataTemplate 指定每个数据项在 StackPanel内显示为三个 TextBlock 元素。The DataTemplate specifies that each data item appears as three TextBlock elements within a StackPanel. 在此示例中,数据对象是一个名为 Task的类。In this example, the data object is a class called Task. 请注意,此模板中的每个 TextBlock 元素都绑定到 Task 类的属性。Note that each TextBlock element in this template is bound to a property of the Task class.

<ListBox Width="400" Margin="10"
         ItemsSource="{Binding Source={StaticResource myTodoList}}">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel>
         <TextBlock Text="{Binding Path=TaskName}" />
         <TextBlock Text="{Binding Path=Description}"/>
         <TextBlock Text="{Binding Path=Priority}"/>
       </StackPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

在 resources 节中定义一个 DataTemplate 更常见,因此它可以是一个可重用对象,如下例所示:It is more common to define a DataTemplate in the resources section so it can be a reusable object, as in the following example:

<Window.Resources>
<DataTemplate x:Key="myTaskTemplate">
  <StackPanel>
    <TextBlock Text="{Binding Path=TaskName}" />
    <TextBlock Text="{Binding Path=Description}"/>
    <TextBlock Text="{Binding Path=Priority}"/>
  </StackPanel>
</DataTemplate>
</Window.Resources>

现在可以将 myTaskTemplate 用作资源,如以下示例所示:Now you can use myTaskTemplate as a resource, as in the following example:

<ListBox Width="400" Margin="10"
         ItemsSource="{Binding Source={StaticResource myTodoList}}"
         ItemTemplate="{StaticResource myTaskTemplate}"/>

有关完整示例,请参阅数据模板化示例简介For the complete sample, see Introduction to Data Templating Sample.

注解

您可以使用 DataTemplate 来指定数据对象的可视化。You use a DataTemplate to specify the visualization of your data objects. ItemsControl (例如 ListBox)绑定到整个集合时,DataTemplate 对象特别有用。DataTemplate objects are particularly useful when you are binding an ItemsControl such as a ListBox to an entire collection. 如果没有特定说明,ListBox 将显示集合中对象的字符串表示形式。Without specific instructions, a ListBox displays the string representation of the objects in a collection. 在这种情况下,可以使用 DataTemplate 来定义数据对象的外观。In that case, you can use a DataTemplate to define the appearance of your data objects. DataTemplate 的内容成为数据对象的可视化结构。The content of your DataTemplate becomes the visual structure of your data objects.

有关详细讨论,请参阅数据模板化概述For an in-depth discussion, see Data Templating Overview.

构造函数

DataTemplate()

初始化 DataTemplate 类的新实例。Initializes a new instance of the DataTemplate class.

DataTemplate(Object)

使用指定的 DataTemplate 属性初始化 DataType 类的新实例。Initializes a new instance of the DataTemplate class with the specified DataType property.

属性

DataTemplateKey

获取 DataTemplate 的默认键。Gets the default key of the DataTemplate.

DataType

获取或设置此 DataTemplate 所针对的类型。Gets or sets the type for which this DataTemplate is intended.

Dispatcher

获取与此 Dispatcher 关联的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(继承自 DispatcherObject)
HasContent

获取一个值,该值指示此模板是否具有优化内容。Gets a value that indicates whether this template has optimized content.

(继承自 FrameworkTemplate)
IsSealed

获取一个值,该值指示此对象是否处于不可变状态,如果处于该状态则无法更改对象。Gets a value that indicates whether this object is in an immutable state so it cannot be changed.

(继承自 FrameworkTemplate)
Resources

获取或设置可在此模板范围内使用的资源集合。Gets or sets the collection of resources that can be used within the scope of this template.

(继承自 FrameworkTemplate)
Template

获取或设置一个引用,该引用指向当编写器定义或应用模板时用于录制或播放模板的 XAML 节点的对象。Gets or sets a reference to the object that records or plays the XAML nodes for the template when the template is defined or applied by a writer.

(继承自 FrameworkTemplate)
Triggers

获取触发器的集合,这些触发器根据一个或多个条件应用属性值或执行操作。Gets a collection of triggers that apply property values or perform actions based on one or more conditions.

VisualTree

获取或设置模板的根节点。Gets or sets the root node of the template.

(继承自 FrameworkTemplate)

方法

CheckAccess()

确定调用线程是否可以访问此 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
FindName(String, FrameworkElement)

查找与此模板中定义的指定名称关联的元素。Finds the element associated with the specified name defined within this template.

(继承自 FrameworkTemplate)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
LoadContent()

将模板的内容加载为对象的实例,并返回该内容的根元素。Loads the content of the template as an instance of an object and returns the root element of the content.

(继承自 FrameworkTemplate)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
RegisterName(String, Object)

将新的名称/对象对注册到当前名称范围内。Registers a new name/object pair into the current name scope.

(继承自 FrameworkTemplate)
Seal()

锁定模板,使其无法更改。Locks the template so it cannot be changed.

(继承自 FrameworkTemplate)
ShouldSerializeResources(XamlDesignerSerializationManager)

返回一个值,该值指示序列化过程是否应序列化此类的实例上的 Resources 属性值。Returns a value that indicates whether serialization processes should serialize the value of the Resources property on instances of this class.

(继承自 FrameworkTemplate)
ShouldSerializeVisualTree()

返回一个值,该值指示序列化过程是否应序列化此类的实例上的 VisualTree 属性值。Returns a value that indicates whether serialization processes should serialize the value of the VisualTree property on instances of this class.

(继承自 FrameworkTemplate)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
UnregisterName(String)

从 XAML 名称范围中移除名称/对象映射。Removes a name/object mapping from the XAML namescope.

(继承自 FrameworkTemplate)
ValidateTemplatedParent(FrameworkElement)

根据一组规则检查模板化父级。Checks the templated parent against a set of rules.

VerifyAccess()

强制调用线程具有此 DispatcherObject 的访问权限。Enforces that the calling thread has access to this DispatcherObject.

(继承自 DispatcherObject)

显式接口实现

INameScope.FindName(String)

返回具有提供的标识名称的对象。Returns an object that has the provided identifying name.

(继承自 FrameworkTemplate)
IQueryAmbient.IsAmbientPropertyAvailable(String)

查询当前范围中是否有指定的环境属性。Queries whether a specified ambient property is available in the current scope.

(继承自 FrameworkTemplate)
ISealable.CanSeal (继承自 FrameworkTemplate)
ISealable.IsSealed (继承自 FrameworkTemplate)
ISealable.Seal() (继承自 FrameworkTemplate)

适用于

另请参阅