- 浏览: 490951 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (1028)
- [发布至博客园首页] (826)
- [随笔分类]个人生活随笔 (14)
- [网站分类]首页候选区 (26)
- [网站分类]SharePoint (15)
- [网站分类]其他技术区 (6)
- [随笔分类]批处理技巧 (6)
- [随笔分类].net 2.0 (3)
- [随笔分类]SharePoint2007(MOSS2007) (0)
- [网站分类].NET新手区 (6)
- [网站分类]ASP.NET (6)
- [网站分类]架构设计 (18)
- [网站分类]程序人生 (2)
- [网站分类]SQL Server (2)
- WCF (3)
- 编程技巧 (2)
- 模式架构 (2)
- 分析设计 (4)
- 生活随笔 (0)
- 软件工程 (1)
- Android实例 (2)
最新评论
-
zilong0536:
楼主您好:
请问发表博文支持图片的功能怎么实现啊,一直没有思路 ...
新浪微博开放平台开发-android客户端(3) -
nicegege:
小弟 学习了
帮助中国移动设计10086的排队小模块 -
zl7824516:
用什么技术没说啊
通告(公告),消息(站内短信),提醒的设计:通告 -
virusswb:
源码下载: SinaWeibo2 源码下载之后,将后缀改为ra ...
新浪微博开放平台开发-android客户端(3) -
Jimmyxu0311:
找不到源码下载
新浪微博开放平台开发-android客户端(3)
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>
发表评论
-
NET 应用架构指导 V2 学习笔记(十六) 服务层设计指导
2010-06-04 00:13 544如果你的应用是通 ... -
NET 应用架构指导 V2 学习笔记(十七) 组件设计指导
2010-06-05 00:48 657组件提供了一种将 ... -
NET 应用架构指导 V2 学习笔记(十八) 表现层组件设计指导
2010-06-05 21:09 518本章讲述的是你在设计用户界面组件和表现层逻辑组件的时候应该 ... -
NET 应用架构指导 V2 学习笔记(十九) 表现层组件设计指导
2010-06-06 06:15 5805 决定数据绑定的 ... -
NET 应用架构指导 V2 学习笔记(二十) 业务组件设计指导
2010-06-07 06:58 606前言 业务组件 ... -
微软企业库5.0学习笔记(四十二)异常处理模块
2010-06-14 00:04 828企业库的异常处理 ... -
关于程序员在30岁、35岁之后怎么办的新思考
2010-06-14 10:40 616首先给大家问个好 ... -
NET 应用架构指导 V2 学习笔记(二十四) 跨层关注问题
2010-06-17 20:00 580概况 大部分的 ... -
微软企业库5.0学习笔记(四十三)数据验证模块
2010-06-19 08:07 988概况 任何接受用户或者是其他系统输入的应用,一定要确保 ... -
关于项目进度慢的思考----如何提高整体开发效率
2010-06-21 23:42 793我们都是软件行业 ... -
微软企业库5.0学习笔记(四十四)实战数据验证模块
2010-06-23 19:22 8271 在业务对象上添加验证 添加对程序集【Microso ... -
微软企业库5.0学习笔记(四十五)实战数据验证模块----高级篇
2010-06-24 19:41 9651、添加自定义的提示信息 验证失败的提示信息可以自定义 ... -
面向对象类设计的五大原则(一)单一职责原则Single Responsibility Principle
2010-06-29 15:45 767引言 面向对象类设计,或者说是面向对象设计,有五大原则 ... -
《深入浅出设计模式-中文版》读书笔记 开篇乱弹(一)
2010-07-01 06:42 640oreilly的《Head.First ... -
《深入浅出设计模式-中文版》读书笔记-继承与组合(三)
2010-07-03 16:53 597经过上一次的改造 ... -
《深入浅出设计模式-中文版》读书笔记-观察者模式(四)
2010-07-06 06:34 622今天要接触的是观 ... -
利用attribute实现简单的ORM
2010-07-09 15:27 671我不知道NH的ORM具 ... -
系统内部模块(子系统)之间的耦合以及模块(子系统)划分
2010-07-14 13:02 803题外话 最近已经在努力学习了,学习基本功,学习设计模式 ... -
《深入浅出设计模式-中文版》读书笔记-工厂模式(五)
2010-07-16 12:46 687今天给大家带来的是:工厂模式。 我们在代码中创建一个对 ... -
Head.First.Object-Oriented.Design.and.Analysis《深入浅出面向对象的分析与设计》读书笔记(一)
2010-07-18 21:47 664题外话 又是一本Head.First系列的书,这个系列 ...
相关推荐
在这个"Silverlight绑定数据的例子.zip"压缩包中,我们很可能会找到一个演示如何在Silverlight应用中实现数据绑定的实例。 在Silverlight中,数据绑定主要通过XAML(Extensible Application Markup Language)来...
7. **数据绑定(Data Binding)** 如果图像数据来源于应用程序的数据模型,可以使用 Silverlight 的数据绑定功能将 Image 控件的 Source 属性绑定到模型中的图像源属性,实现动态加载和更新。 8. **样式和模板...
1. **定义DataTemplates**:首先,你需要为每种视图创建一个DataTemplate。每个模板都应包含一组UI元素(如TextBlock、Image、Button等),这些元素根据数据模型进行绑定。你可以将DataTemplate定义在XAML文件中,...
##### 隐式数据模板 (Implicit Data Templates) 隐式数据模板允许开发者在不显式定义数据模板的情况下自动为不同类型的数据项设置默认显示方式。这简化了 XAML 代码并提高了开发效率。 示例代码如下: ```xml ...
视图可以通过数据模板(Data Templates)和控件模板(Control Templates)来决定如何显示数据和UI元素。这使得视图模型可以专注于业务逻辑,而无需关心具体的UI样式。 **8. 路由和导航(Routing and Navigation)**...
**数据绑定(Data Binding)** - **数据源**:介绍如何连接到各种数据源,如数据库、XML文件等。 - **双向绑定**:教授如何实现数据的双向同步,使UI元素能够及时反映数据变化。 #### 20. **数据控件(Data ...
##### 第20章:Data Binding - **数据绑定机制**:掌握数据绑定的基本原理及其应用场景。 - **双向绑定**:实现视图与模型之间的实时同步更新。 ##### 第21章:Data Controls - **数据控件**:介绍各种用于显示和...
在Windows Phone 7 (WP7) 开发中,数据绑定是构建用户界面的重要技术,而数据模板则是将数据对象转化为可视元素的关键工具。本篇文章主要探讨如何根据数据对象的类型动态地应用数据模板,这对于创建灵活且可扩展的UI...
综上所述,格式化Silverlight TextBlock控件涉及多个方面,包括基本样式属性的设置、数据绑定、模板应用、复杂文本格式化以及事件处理。通过熟练掌握这些技巧,开发者可以创造出更加美观且功能丰富的用户界面。参考...
简单来说:声明你的数据作为一个JavaScript 模型对象(model object),然后将DOM 元素或者模板(templates)绑定到它上面. The quickest and most fun way to get started is by working through the interactive ...
2. **数据绑定(Data Binding)**:WPF的核心特性,允许UI元素(如组合框)自动显示和更新与其绑定的数据源中的数据。 3. **组合框(ComboBox)**:WPF中的控件,用于让用户在下拉列表中选择一个项。它可以显示当前...
- **DataSets and Data Binding** (第21章): 介绍了数据集和数据绑定的概念及其实现方法。 - **Visual Database Tools** (第22章): 详细介绍了Visual Studio中的数据库工具,包括数据库项目的创建和管理。 - **...