8

和有什么区别

  • 控制模板
  • 数据模板
  • 分层数据模板
  • 项目模板
4

2 回答 2

8

控制模板

ControlTemplate 指定控件的视觉结构和视觉行为。您可以通过给它一个新的 ControlTemplate 来自定义控件的外观。创建 ControlTemplate 时,您替换现有控件的外观而不更改其功能。例如,您可以将应用程序中的按钮设为圆形而不是默认的方形,但按钮仍会引发 Click 事件。

ControlTemplate 的一个例子是

创建一个按钮

<Button Style="{StaticResource newTemplate}" 
        Background="Navy" 
        Foreground="White" 
        FontSize="14"
        Content="Button1"/>

按钮的 ControlTemplate

<Style TargetType="Button" x:Key="newTemplate"> 
      <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="RootElement">
                <!--Create the SolidColorBrush for the Background 
                as an object elemment and give it a name so 
                it can be referred to elsewhere in the control template.-->
                    <Border.Background>
                        <SolidColorBrush x:Name="BorderBrush" Color="Black"/>
                    </Border.Background>
                    <!--Create a border that has a different color by adding smaller grid.
                    The background of this grid is specificied by the button's Background
                    property.-->
                    <Grid Margin="4" Background="{TemplateBinding Background}">
                    <!--Use a ContentPresenter to display the Content of
                    the Button.-->
                        <ContentPresenter
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        Margin="4,5,4,4" />
                    </Grid>
                </Border>
            </ControlTemplate>      
        </Setter.Value>
    </Setter>
</Style>

更多关于ControlTemplate

数据模板

数据模板是与控制模板类似的概念。它们为您提供了一个非常灵活且功能强大的解决方案,用于替换 ListBox、ComboBox 或 ListView 等控件中数据项的视觉外观。WPF 控件具有支持自定义数据表示的内置功能。

DataTemplate 的一个例子是

<!-- Without DataTemplate -->
<ListBox ItemsSource="{Binding}" /> 

<!-- With DataTemplate -->
<ListBox ItemsSource="{Binding}" BorderBrush="Transparent" 
         Grid.IsSharedSizeScope="True"
         HorizontalContentAlignment="Stretch">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Margin="4">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Key" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Text="{Binding Name}" FontWeight="Bold"  />
                <TextBox Grid.Column="1" Text="{Binding Value }" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

有关数据模板和触发器的更多信息

项目模板

您使用 ItemTemplate 来指定数据对象的可视化。如果您的 ItemsControl 绑定到集合对象,并且您没有使用 DataTemplate 提供特定的显示指令,则每个项目的结果 UI 都是基础集合中每个对象的字符串表示形式。

项目模板的一个例子是

<ListBox Margin="10" Name="lvDataBinding">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <TextBlock Text="Name: " />
                <TextBlock Text="{Binding Name}" FontWeight="Bold" />
                <TextBlock Text=", " />
                <TextBlock Text="Age: " />
                <TextBlock Text="{Binding Age}" FontWeight="Bold" />
                <TextBlock Text=" (" />
                <TextBlock Text="{Binding Mail}" TextDecorations="Underline" Foreground="Blue" Cursor="Hand" />
                <TextBlock Text=")" />
            </WrapPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

在 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 属性。

有关详细信息,请参阅数据模板概述。

于 2015-09-22T04:56:18.827 回答
2
  1. ControlTemplaes 定义控件的“外观”和“行为”。默认情况下,按钮是矩形的。默认情况下,ListBox 具有白色背景。这些都是由 Control 的 ControlTemple 定义的。

  2. DataTemplae 通过它所拥有的数据布局来帮助控件。如果将用户列表添加到列表框中,并且您希望 UserName 出现在 UserPassword 之前,那么您将在 DataTemples 中定义它。DataTemples 分配给 ListBox 的 ItemTemplate (4) 属性。

  3. HierarchalDataTemplte 与 DataTemples 相同,只是它处理层次数据源。它通常与 TreeView 控件一起使用。

于 2013-02-19T18:42:10.357 回答