Silverlight3系列(八)数据绑定 Data Binding 3 数据模板 Data Templates
2010-01-29 14:05 Virus-BeautyCode 阅读(1841) 评论(0) 编辑 收藏 举报8 数据模板
数据模板在xaml标记中是比较重要的,它定义了绑定对象如何显示。一共有两种类型的控件支持数据模板:
1)内容控件(具有Content属性的控件)通过ContentTemplate属性支持数据模板。用来显示你放在Content属性中的任何东西。
2)列表控件(从ItemsControl中继承而来的控件)通过ItemTemplate属性支持数据绑定。这个模板用来显示集合(你提供给ItemsSource属性的对象集合)中每一个Item。
列表模板是以内容模板为基础的,就好像ListBox的ListBoxItem,ComboBox的ComboBoxItem,等等。无论你是用什么ItemTemplate,你每一Item还是可以使用ContentTemplate。
<ListBox >
<ListBox.ItemTemplate>
<DataTemplate>
<ScrollViewer>
<TextBlock Text="{Binding Name}"></TextBlock>
</ScrollViewer>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
当你绑定产品集合Products到一个ListBox的时候(通过设置ItemsSource),每一个ListBoxItem都是一个Product对象。ListBoxItem.Content属性设置为一个Product对象,ListBoxItem.ContentTemplate来控制数据的显示,上面是绑定到产品名称Name。
8.1 分离重用模板
就像样式一样,模板通常是定义在页面或者应用级别,而不是定义在一个list上面。分离通常是比较好的,尤其是你的模板很长,很复杂,或者在一个控件上面使用多个模板。同时你也可以统一你的界面风格,在任何地方都可以使用这个模板。
需要这么做的的话,你需要的就是在资源集合Resources Collection中定义并且给它一个唯一的名字。下面定义了一个模板资源

<DataTemplate x:Key="ProductDataTemplate">
<Border Margin="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="5">
<Grid Margin="3">
<Grid.RowDefinitions >
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock FontWeight="Bold" Text="{Binding ModelNumber}"></TextBlock>
<TextBlock FontWeight="Bold" Grid.Row="1" Text="{Binding ModelName}"></TextBlock>
</Grid>
</Border>
</DataTemplate>
</UserControl.Resources>
下面是使用方法
<ListBox Name="lstProducts" x:Name="lstProducts" HorizontalAlignment="Center"
ItemTemplate="{StaticResource ProductDataTemplate}"></ListBox>
Data templates不需要数据绑定,换句话说,你不需要ItemsSource属性来绑定数据到ListBox,你可以自己调用ListBox.Items.Add()方法。
8.2 高级绑定
当你使用一些基本控件,例如TextBlock,和数据绑定表达式expression的时候,你可以使用更复杂的控制功能,加事件处理,转换数据类型,和使用动画效果。

<loc:ImagePathConverter x:Key="ImagePathConverter"></loc:ImagePathConverter>
<DataTemplate x:Key="ProductDataTemplate">
<Border Margin="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="5">
<Grid Margin="3">
<Grid.RowDefinitions >
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock FontWeight="Bold" Text="{Binding ModelNumber}"></TextBlock>
<TextBlock FontWeight="Bold" Grid.Row="1" Text="{Binding ModelName}"></TextBlock>
<Image Margin="5" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left"
Source="{Binding ProductImagePath, Mode=TwoWay, Converter={StaticResource ImagePathConverter}}"></Image>
</Grid>
</Border>
</DataTemplate>
</UserControl.Resources>
上面的绑定中实现了数据绑定和类型转换。
如果你的模板有错误,你不会收到任何异常,这时候控件不会显示数据,而是会空白。
8.3 改变列表项的布局

<ListBox.ItemsPanel>
<ItemsPanelTemplate >
<controlsToolkit:WrapPanel></controlsToolkit:WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构