`

Silverlight3系列(八)数据绑定 Data Binding 3 数据模板 Data Templates

 
阅读更多

  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中定义并且给它一个唯一的名字。下面定义了一个模板资源

  

 

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><UserControl.Resources >
                
<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的时候,你可以使用更复杂的控制功能,加事件处理,转换数据类型,和使用动画效果。

  

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--> <UserControl.Resources >
        
<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 改变列表项的布局

  

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><ListBox  Margin="7,3,7,10" Name="lstProducts" ItemTemplate="{StaticResource ProductDataTemplate}">
            
<ListBox.ItemsPanel>
                
<ItemsPanelTemplate >
                    
<controlsToolkit:WrapPanel></controlsToolkit:WrapPanel>
                
</ItemsPanelTemplate>
            
</ListBox.ItemsPanel> 
        
</ListBox>

 

  
分享到:
评论

相关推荐

    Silverlight绑定数据的例子.zip

    在这个"Silverlight绑定数据的例子.zip"压缩包中,我们很可能会找到一个演示如何在Silverlight应用中实现数据绑定的实例。 在Silverlight中,数据绑定主要通过XAML(Extensible Application Markup Language)来...

    Silverlight示例6

    7. **数据绑定(Data Binding)** 如果图像数据来源于应用程序的数据模型,可以使用 Silverlight 的数据绑定功能将 Image 控件的 Source 属性绑定到模型中的图像源属性,实现动态加载和更新。 8. **样式和模板...

    Silverlight实例秀——可切换视图的DataTemplate

    1. **定义DataTemplates**:首先,你需要为每种视图创建一个DataTemplate。每个模板都应包含一组UI元素(如TextBlock、Image、Button等),这些元素根据数据模型进行绑定。你可以将DataTemplate定义在XAML文件中,...

    silverlight_5_beta_features

    ##### 隐式数据模板 (Implicit Data Templates) 隐式数据模板允许开发者在不显式定义数据模板的情况下自动为不同类型的数据项设置默认显示方式。这简化了 XAML 代码并提高了开发效率。 示例代码如下: ```xml ...

    MVVM详细教程提高篇

    视图可以通过数据模板(Data Templates)和控件模板(Control Templates)来决定如何显示数据和UI元素。这使得视图模型可以专注于业务逻辑,而无需关心具体的UI样式。 **8. 路由和导航(Routing and Navigation)**...

    Pro Silverlight 5 in CSharp 4th edition

    **数据绑定(Data Binding)** - **数据源**:介绍如何连接到各种数据源,如数据库、XML文件等。 - **双向绑定**:教授如何实现数据的双向同步,使UI元素能够及时反映数据变化。 #### 20. **数据控件(Data ...

    Pro Silverlight 5 in C# (2012)

    ##### 第20章:Data Binding - **数据绑定机制**:掌握数据绑定的基本原理及其应用场景。 - **双向绑定**:实现视图与模型之间的实时同步更新。 ##### 第21章:Data Controls - **数据控件**:介绍各种用于显示和...

    在WP7中按类型动态应用数据模板

    在Windows Phone 7 (WP7) 开发中,数据绑定是构建用户界面的重要技术,而数据模板则是将数据对象转化为可视元素的关键工具。本篇文章主要探讨如何根据数据对象的类型动态地应用数据模板,这对于创建灵活且可扩展的UI...

    提示:格式化Silverlight TextBlock控件

    综上所述,格式化Silverlight TextBlock控件涉及多个方面,包括基本样式属性的设置、数据绑定、模板应用、复杂文本格式化以及事件处理。通过熟练掌握这些技巧,开发者可以创造出更加美观且功能丰富的用户界面。参考...

    Knockout API 中文版

    简单来说:声明你的数据作为一个JavaScript 模型对象(model object),然后将DOM 元素或者模板(templates)绑定到它上面. The quickest and most fun way to get started is by working through the interactive ...

    时髦的WPF-枚举和组合框

    2. **数据绑定(Data Binding)**:WPF的核心特性,允许UI元素(如组合框)自动显示和更新与其绑定的数据源中的数据。 3. **组合框(ComboBox)**:WPF中的控件,用于让用户在下拉列表中选择一个项。它可以显示当前...

    Wrox.Professional.Visual.Studio.2008.Jul.2008

    - **DataSets and Data Binding** (第21章): 介绍了数据集和数据绑定的概念及其实现方法。 - **Visual Database Tools** (第22章): 详细介绍了Visual Studio中的数据库工具,包括数据库项目的创建和管理。 - **...

Global site tag (gtag.js) - Google Analytics