WPF中的各种Template

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,并将 ListBoxItem 的 DataContext 设置为相应的客户。也就是说,第一个 ListBoxItem 的 DataContext 设置为第一个客户,第二个 ListBoxItem 的 DataContext 设置为第二个客户,依此类推。可以将 DataTemplate 中的元素绑定到 Customer 对象的属性。

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

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

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

如下:

ContractedBlock.gif ExpandedBlockStart.gif 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上的描述:

您使用 ItemTemplate 来指定数据对象的可视化。 如果您的 ItemsControl 绑定到一个集合对象,并且您未使用 DataTemplate 提供明确的显示说明,则每个项的结果 UI 将是基础集合中每个对象的字符串表示形式。

当您在 ItemsControl 上设置 ItemTemplate 时,将生成如下所示的 UI(以 ListBox 为例):

  1. 在内容生成过程中,ItemsPanel 将启动一个 ItemContainerGenerator 请求以便为每个数据项创建一个容器。 对于 ListBox,容器是一个 ListBoxItem。 生成器回调 ItemsControl 以准备该容器。

  2. 部分准备工作涉及将 ListBox 的 ItemTemplate 复制为 ListBoxItem 的 ContentTemplate。

  3. 与所有 ContentControl 类型类似,ListBoxItem 的 ControlTemplate 也包含一个 ContentPresenter。 应用该模板时,它会创建一个 ContentPresenter,其 ContentTemplate 绑定到 ListBoxItem 的 ContentTemplate。

  4. 最后,ContentPresenter 将该 ContentTemplate 应用于自身,由此创建 UI。

如果您定义了多个 DataTemplate,并且希望提供逻辑以便以编程方式选择和应用 DataTemplate,请使用 ItemTemplateSelector 属性。

ItemsControl 为可视化自定义提供了很大灵活性,并提供了许多样式和模板属性。 使用 ItemContainerStyle 属性或 ItemContainerStyleSelector 属性来设置样式,以影响包含数据项的元素的外观。 例如,对于 ListBox,生成的容器是 ListBoxItem 控件;对于 ComboBox,它们是 ComboBoxItem 控件。 若要影响项的布局,请使用 ItemsPanel 属性。 如果在控件上使用分组,可以使用 GroupStyle 或 GroupStyleSelector 属性。

6. ItemsPanelTemplate

指定 ItemsPresenter 为 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 { ... }

ItemsControl 类型具有一个类型为 ItemsPanelTemplate 的 ItemsPanel 属性。

每种 ItemsControl 类型都有默认的 ItemsPanelTemplate 对于 ItemsControl 类,默认的 ItemsPanel 值为指定 StackPanel 的 ItemsPanelTemplate 对于 ListBox,默认值使用 VirtualizingStackPanel。 对于 MenuItem,默认值使用 WrapPanel。 对于 StatusBar,默认值使用 DockPanel。 如下用ILSpy查看的:

posted on 2011-09-05 11:20 cutebear 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/bear831204/archive/2011/09/05/2167151.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值