1. FrameworkTemplate

2. ControlTemplate - 控件模板

允许您指定控件的可视结构。

public class ControlTemplate : FrameworkTemplate {...}

3. DataTemplate - 数据模板

描述数据对象的可视结构。

public class DataTemplate : FrameworkTemplate {...}

作用于所有继承自ContentControl的内容控件的ContentTemplate属性和所有继承自ItemsControl的列表控件的ItemTemplate属性,即用于设置控件的数据内容。官方MSDN描述如下:

通常使用 DataTemplate 指定数据的直观表示。当您将 ItemsControl(如 ListBox)绑定到整个集合时,DataTemplate 对象尤其有用。如果没有特殊说明,ListBox 将在集合中显示对象的字符串表示形式。在此情况下,可以使用 DataTemplate 定义数据对象的外观。DataTemplate 的内容变成数据对象的可视结构。

可以在 DataTemplate 中使用数据绑定。例如,假定 ListBox 绑定到 Customer 对象的集合,并且将 ItemTemplate 属性设置为 DataTemplate。创建 ListBox 时,将为集合中的每个 Customer 创建一个 ListBoxItem,并将 ListBoxItemDataContext 设置为相应的客户。也就是说,第一个 ListBoxItemDataContext 设置为第一个客户,第二个 ListBoxItemDataContext 设置为第二个客户,依此类推。可以将 DataTemplate 中的元素绑定到 Customer 对象的属性。

还可以使用 DataTemplate 在多个 ContentControl 对象之间共享 UIElement 对象。例如,假设需要应用程序上的多个按钮具有相同的图形。可以创建一个包含此图形的 DataTemplate,并将它用作这些按钮的 ContentTemplate。有关更多信息,请参见 ContentControl.ContentTemplate

可以将 DataTemplate 作为 object.ItemTemplate 属性元素的直接子级。还可以定义一个 DataTemplate 作为资源,然后将该资源作为 ItemTemplate 属性的值引用。

定义用于创建数据模板的内容的 XAML 用法不作为可设置的属性公开。这是内置于 DataTemplate 对象元素的 XAML 处理的特殊行为。

如下:

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

4. ContentControl.ContentTemplate

ContentControl 内容的数据模板。它是ContentControl的一个属性,返回值是DataTemplate类型。

public class ContentControl : Control, IAddChild
{
   ...
  
//
  
// Summary:
  
// Gets or sets the data template used to display the content of the System.Windows.Controls.ContentControl.
  
//
  
// Returns:
  
// A data template. The default value is null.
   [Bindable(true)]
   [CustomCategory(
"Content")]
  
public DataTemplate ContentTemplate { get; set; }

   ...
}


 5. ItemsControl.ItemTemplate

DataTemplate。它是ItemsControl的一个属性,返回值是DataTemplate类型。

public class ItemsControl : Control, IAddChild, IGeneratorHost
{
...

//
// Summary:
// Gets or sets the System.Windows.DataTemplate used to display each item.
//
// Returns:
// A System.Windows.DataTemplate that specifies the visualization of the data
// objects. The default is null.
[CustomCategory("Content")]
[Bindable(
true)]
public DataTemplate ItemTemplate { get; set; }

...
}

下面是msdn上的描述:

DataTemplate 提供明确的显示说明,则每个项的结果 UI 将是基础集合中每个对象的字符串表示形式。

ListBox 为例):

  1. ItemsControl 以准备该容器。

  2. ContentTemplate

  3. ContentTemplate

  4. ContentTemplate 应用于自身,由此创建 UI。

ItemTemplateSelector 属性。

GroupStyleSelector 属性。

6. ItemsPanelTemplate

ItemsControl 的项的布局创建的面板。

// Summary:
// Specifies the panel that the System.Windows.Controls.ItemsPresenter creates
// for the layout of the items of an System.Windows.Controls.ItemsControl.
public class ItemsPanelTemplate : FrameworkTemplate { ... }

ItemsPanel 属性。

DockPanel。 如下用ILSpy查看的:

WPF中的各种Template

相关文章: