WPF中控件模板和DataTemplate之间的区别

问题描述 投票:188回答:6

在WPF中ControlTemplateDataTemplate有什么区别?

wpf datatemplate controltemplate
6个回答
255
投票

通常,控件是为了它自己而呈现的,并不反映底层数据。例如,Button不会绑定到业务对象 - 它纯粹是因为它可以被点击。但是,ContentControlListBox通常会出现,以便他们可以为用户提供数据。

因此,DataTemplate用于为底层数据提供可视化结构,而ControlTemplate与底层数据无关,只是为控件本身提供可视化布局。

ControlTemplate通常只包含TemplateBinding表达式,绑定回控件本身的属性,而DataTemplate将包含标准Binding表达式,绑定到其DataContext(业务/域对象或视图模型)的属性。


106
投票

非常基本上,ControlTemplate描述了如何在DataTemplate描述如何显示数据时显示控件。

例如:

一个Label是一个对照,将包括一个ControlTemplate,其中说Label应该使用Border围绕一些内容(DataTemplate或另一个控制)显示。

Customer类是数据,将使用DataTemplate显示,可以说显示Customer类型为StackPanel,其中包含两个显示名称的TextBlocks,另一个显示电话号码。注意所有类都使用DataTemplates显示可能会有所帮助,您通常只使用默认模板TextBlock,其中Text属性设置为Object的ToString方法的结果。


31
投票

Troels LarsenMSDN forum有一个很好的解释

<Window x:Class="WpfApplication7.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <DataTemplate x:Key="ButtonContentTemplate">
      <StackPanel Orientation="Horizontal">
        <Grid Height="8" Width="8">
          <Path HorizontalAlignment="Stretch" 
           Margin="0,0,1.8,1.8" 
           VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FF000000" 
           Data="M0.5,5.7 L0.5,0.5 L5.7,0.5"/>
          <Path HorizontalAlignment="Stretch" 
           Margin="2,3,0,0" 
           VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FFFFFFFF" 
           Data="M3.2,7.5 L7.5,7.5 L7.5,3.5"/>
          <Path HorizontalAlignment="Stretch" 
           Margin="1.2,1.4,0.7,0.7" 
           VerticalAlignment="Stretch" Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" 
           Data="M2.5,2.5 L7.5,7.5"/>
          <Path HorizontalAlignment="Stretch" 
           Margin="1.7,2.0,1,1" 
           VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FF000000" 
           Data="M3,7.5 L7.5,7.5 L7.5,3.5"/>
          <Path HorizontalAlignment="Stretch" 
           Margin="1,1,1,1" 
           VerticalAlignment="Stretch" Stretch="Fill" Stroke="#FFFFFFFF" 
           Data="M1.5,6.5 L1.5,1 L6.5,1.5"/>
        </Grid>
        <ContentPresenter Content="{Binding}"/>
      </StackPanel>
    </DataTemplate>
    <ControlTemplate TargetType="Button" x:Key="ButtonControlTemplate">
      <Grid>
        <Ellipse Fill="{TemplateBinding Background}"/>
        <ContentPresenter HorizontalAlignment="Center"
              VerticalAlignment="Center"/>
      </Grid>
    </ControlTemplate>
  </Window.Resources>
  <StackPanel>
    <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="1"/>
    <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="2"/>
    <Button Template="{StaticResource ButtonControlTemplate}" ContentTemplate="{StaticResource ButtonContentTemplate}" Content="3"/>
  </StackPanel>
</Window>

(分别从http://msdn.microsoft.com/en-us/library/system.windows.controls.controltemplate.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contenttemplate%28VS.95%29.aspx偷来的模板)

无论如何,ControlTemplate决定Button本身的外观,而ContentTemplate决定按钮内容的外观。因此,您可以将内容绑定到其中一个数据类,并让它自己呈现,但是您需要它。


18
投票

ControlTemplate:代表控制风格。

DataTemplate:表示数据样式(您希望如何显示数据)。

所有控件都使用您可以通过模板属性覆盖的默认控件模板。

例如 Button模板是一个控件模板。 Button内容模板是一种数据模板

<Button   VerticalAlignment="Top" >
    <Button.Template>
        <ControlTemplate >
            <Grid>
                <Rectangle Fill="Blue" RadiusX="20" RadiusY="20"/>
                <Ellipse Fill="Red" />
                <ContentPresenter Content="{Binding}">
                    <ContentPresenter.ContentTemplate>
                        <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="50">
                            <TextBlock Text="Name" Margin="5"/>
                                <TextBox Text="{Binding UserName, Mode=TwoWay}" Margin="5" Width="100"/>
                            <Button Content="Show Name" Click="OnClickShowName" />
                        </StackPanel>
                    </DataTemplate>
                    </ContentPresenter.ContentTemplate>
                </ContentPresenter>
            </Grid>
        </ControlTemplate>
    </Button.Template>
</Button>

public String UserName
{
    get { return userName; }
    set
    {
        userName = value;
        this.NotifyPropertyChanged("UserName");
    }
}

6
投票

ControlTemplate - 改变元素的外观。例如,Button可以包含图像和文本

DataTemplate - 使用元素表示基础数据。


1
投票

ControlTemplate定义了视觉外观,DataTemplate替换了数据项的视觉外观。

示例:我想显示一个从矩形到圆形的按钮=>控制模板。

如果你有对象的复杂对象,它只是调用并显示ToString(),使用DataTemplate你可以获得各种成员并显示和更改它们的数据对象的值。

© www.soinside.com 2019 - 2024. All rights reserved.