WPF:Data Template
来源:互联网 发布:网络黑彩票代理犯法吗 编辑:程序博客网 时间:2024/06/01 09:15
Data Template 用于在一个或者多个控件上绑定并映射数据显示,适用于内容控件或者条目控件, Data Template 可以被用在两个地方:
- 作为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>
0 0
- WPF:Data Template
- WPF Template
- wpf 自定义Tooltip template
- WPF Expander-Template(+-)
- WPF中的Template模板
- WPF Style & Template
- WPF Expander-Template(default)
- WPF data-binding
- WPF关于C#代码实现Template
- Building Custom Template-able WPF Controls
- WPF/Silverlight Template使用及总结
- WPF中各个Template的分析
- WPF Template,ItemsPresenter,ItemContainerStyle,ItemsPanel,contentpresenter
- WPF : ListBox的几种Template属性
- WPF Template模版之Style【四】
- WPF-ListBox的几种Template属性
- WPF 构建自定义控件 模板Template
- WPF - Data Binding入门概述
- 理解Semaphore及其用法详解
- mybatis常用jdbcType数据类型 对应java类型
- 免费分享到处在卖的最新微商管家4.0
- studio 或Android 一些搞笑的bug
- js实验2.(1)幻灯片切换实现
- WPF:Data Template
- 灰度发布系统的实现
- 二叉排序树的建立和遍历(java)
- C++实现从字符串中查找是否含有另一段字符串
- 集成ping++支付
- 从【状态模式】解析App登录功能实战技巧
- 初学Linux系统最应该做对的4件事情[长文]
- adb相关命令
- 三分钟了解activiti