WPF:Data Template

来源:互联网 发布:网络黑彩票代理犯法吗 编辑:程序博客网 时间:2024/06/01 09:15

  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>


0 0