WPF:Data Template

  Data Template 用于在一个或者多个控件上绑定并映射数据显示,适用于内容控件或者条目控件, Data Template 可以被用在两个地方:

  1. 作为ContentControl的ContentTemplate 属性的值(例如:label)
  2. 作为ItemsControl的ItemTemplate属性的值(例如:ListBox)
 下面是一个作为Lable控件的ContentTemplate的例子,假定存在数据类Temp:
<Label Name="lblPerson">
    <Label.ContentTemplate>
        <DataTemplate>
            <Border BorderThickness="2" BorderBrush="DarkBlue">
                <StackPanel Orientation="Vertical">
                    <StackPanel Orientation="Horizontal">
                        <Label Content="{Binding Path=Temp.FirstName}"/>
                        <Label Content="{Binding Path=Temp.LastName}" FontWeight="Bold"/>
                    </StackPanel>
                    <Label Content="{Binding Path=Temp.BirthYear}" FontStyle="Italic"/>
                </StackPanel>
            </Border>
        </DataTemplate>
    </Label.ContentTemplate>
</Label>

这是一个作为ListBox控件的ItemTemplate的例子,假定存在数据集合 ActorList

<ListBox Margin="15" Width="270" Height="320"
       ItemsSource="{Binding ActorList}">
      <ListBox.ItemTemplate>
          <DataTemplate>
              <StackPanel Orientation="Horizontal">
                  <Image Source="{Binding Image}" Height="80"/>
                  <StackPanel Margin="5">
                      <TextBlock Text="{Binding FullName}" FontSize="12" FontWeight="Bold"/>
                      <TextBlock Text="{Binding Dates}"/>
                      <TextBlock Text="{Binding KnownFor}" Margin="0,5,0,0" FontStyle="Italic"/>
                  </StackPanel>
                  <Label Content="Dead Fred !" Foreground="Red"
                         FontWeight="Bold"
                         Visibility="{Binding Converter={StaticResource deadFredConverter}}"/>
              </StackPanel>
          </DataTemplate>
      </ListBox.ItemTemplate>
  </ListBox>

  ContentTemplate 与ItemTemplate 属性经常被用在以下方面:
  1. ContentTemplate 作为ContentPresenter内容,控件的控件模板中默认包含ContentPresenter
  2. ItemTemplate 作为由ItemsPresenter提供的每一条目的内容模板。

 下面是一个Label控件的默认控件模板代码片段,能够发现它包含一个ContentPresenter作为控件模板的内容模板。

<ControlTemplate TargetType="{x:Type Label}">
    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
        <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值