zoukankan      html  css  js  c++  java
  • 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,并将 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上的描述:

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

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

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

    2. 部分准备工作涉及将 ListBoxItemTemplate 复制为 ListBoxItemContentTemplate

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

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

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

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

    6. ItemsPanelTemplate

    指定 ItemsPresenterItemsControl 的项的布局创建的面板。

    // 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 类型具有一个类型为 ItemsPanelTemplateItemsPanel 属性。

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

  • 相关阅读:
    移动端 h5开发相关内容总结——CSS篇
    水滴导航特效
    腾讯课堂之前端开发html5css3javascriptjQueryJS年薪20万
    阿里前端笔试总结
    Web 开发的未来:React、Falcor 和 ES6
    Yii2按需加载图片怎么做?
    Yii2 灵活加载js、css
    Ubuntu上搭建SVN
    yii pageTitle与Yii::app()->name的区别
    Mysql--Database Exception (#42) 数据库错误
  • 原文地址:https://www.cnblogs.com/bear831204/p/2167151.html
Copyright © 2011-2022 走看看