WPF:Data Template
2017-04-17 10:50
323 查看
Data Template 用于在一个或者多个控件上绑定并映射数据显示,适用于内容控件或者条目控件, Data Template 可以被用在两个地方:
作为ContentControl的ContentTemplate 属性的值(例如:label)
作为ItemsControl的ItemTemplate属性的值(例如:ListBox)
下面是一个作为Lable控件的ContentTemplate的例子,假定存在数据类Temp:
这是一个作为ListBox控件的ItemTemplate的例子,假定存在数据集合ActorList:
ContentTemplate 与ItemTemplate 属性经常被用在以下方面:
ContentTemplate 作为ContentPresenter内容,控件的控件模板中默认包含ContentPresenter
ItemTemplate 作为由ItemsPresenter提供的每一条目的内容模板。
下面是一个Label控件的默认控件模板代码片段,能够发现它包含一个ContentPresenter作为控件模板的内容模板。
作为ContentControl的ContentTemplate 属性的值(例如:label)
作为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 属性经常被用在以下方面:
ContentTemplate 作为ContentPresenter内容,控件的控件模板中默认包含ContentPresenter
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>
相关文章推荐
- WPF 点击 datatemplate 里面的控件,获取相应绑定的数据
- WPF中DataTemplate基本原理与缺陷分析
- WPF之DataTemplate数据模板
- WPF Template模版之DataTemplate与ControlTemplate的关系
- WPF Template模版之DataTemplate与ControlTemplate【一】
- 菲佣WPF——4(关于DataTemplate(1))
- 【WPF】ListBox使用DataTemplate 以及默认选中第一项Item
- [WPF系列]-数据邦定之DataTemplate 使用 DataTrigger 来应用属性值
- WPF中Treeview使用HierarchicalDataTemplate模板之后内容不充满
- 如何在WPF和Silverlight中取得DataTemplate中的命名元素
- WPF-控件-DataTemplate生成的控件
- WPF DataTemplate
- WPF DataTemplateSelector的使用
- 菲佣WPF——4(关于DataTemplate(2)动态控件添加神器)
- WPF 获得DataTemplate中的控件
- 潜移默化学会WPF(转载篇<一>)--WPF 遍历DataTemplate(获取所有控件)
- WPF Trigger for IsSelected in a DataTemplate for ListBox items
- WPF在代码中创建DataTemplate时候的异常
- WPF中ControlTemplate和DataTemplate的区别
- WPF中的DataTemplate绑定使用的场合