Silverlight实用窍门系列:58.Silverlight中的Binding使用(三)-数据集合绑定

Silverlight实用窍门系列:58.Silverlight中的Binding使用(三)-数据集合绑定,第1张

概述   在本文中将以ListBox为例讲述在Silverlight中Binding数据集合.   在这里我们将实体集的绑定分为三类:      一、直接控件绑定。      二、DataTemplate模板绑定。      三、详细信息绑定。   首先:我们来看第一类直接控件绑定是对控件的ItemsSource属性进行绑定,然后使用SelectedValuePath指定选择值,DisplayMemb

   在本文中将以ListBox为例讲述在Silverlight中Binding数据集合.

  在这里我们将实体集的绑定分为三类:

     一、直接控件绑定。

     二、DataTemplate模板绑定。

     三、详细信息绑定。

  首先:我们来看第一类直接控件绑定是对控件的ItemsSource属性进行绑定,然后使用SelectedValuePath指定选择值,displayMemberPath指定显示值的方式。Xaml代码如下:

 

  <!--第一种:直接绑定一个Collection实体集合-->     <ListBox Height="239" HorizontalAlignment="left" margin="112,25,0"              name="lbCollection" VerticalAlignment="top" WIDth="198"               ItemsSource="{Binding}" SelectedValuePath="Author" displayMemberPath="name" />   

  其次:DataTemplate是对象制作一个数据模板,所以的数据实体都安装这个数据模板来呈现,现在我们看看其Xaml代码如下:

 

  <!--第二种:使用DataTemplate绑定一个Collection实体集合-->       <ListBox Height="239" HorizontalAlignment="left" margin="478,0"                 name="lbTemplate" ItemsSource="{Binding}" VerticalAlignment="top" WIDth="198" >           <ListBox.ItemTemplate>               <DataTemplate>                   <StackPanel OrIEntation="Horizontal" margin="3"                      <sdk:Label  Content="Docname:"></sdk:Label>                       <TextBlock Text="{Binding name}"></TextBlock>                       <sdk:Label  Content="  Author:"></sdk:Label>                       <TextBlock Text="{Binding Author}"></TextBlock>                   </StackPanel>               </DataTemplate>           </ListBox.ItemTemplate>       </ListBox>   

  最后:详细信息绑定是当用户点击列表中某一个实体标题属性的时候,自动显示其实体的详细信息给用户观看,注意在这里列表的数据源以及详细信息显示页的数据源都必须是CollectionVIEwSource类型的,其Xaml代码如下:

 

  <!--第三种:使用绑定一个Detail详细信息-->     <StackPanel HorizontalAlignment="left"  OrIEntation="Horizontal" VerticalAlignment="top"                 WIDth="500" Height="240" margin="112,294,188,66"        <ListBox Height="239" HorizontalAlignment="left" name="lbDetail"                   VerticalAlignment="top" WIDth="198" ItemsSource="{Binding}"                  SelectedValuePath="Author" displayMemberPath="name" />              <StackPanel x:name="spDetail"  WIDth="300" Height="200"            <TextBlock FontWeight="Bold" Text="{Binding name}" />             <TextBlock FontStyle="Italic" Text="{Binding Author}"/>             <TextBlock Text="{Binding Content}" />             <TextBlock Text="{Binding WriteDate}" />         </StackPanel>     </StackPanel>   

  本实例的后台代码如下,注意第三种详细信息的绑定方式:

 

  public partial class MainPage : UserControl             public MainPage()         {             InitializeComponent();             //获取实体集合             ObservableCollection<Info> List = Info.GetList();                  //第一种数据源赋值             this.lbCollection.DataContext = List;                  //第二种数据源赋值             this.lbTemplate.DataContext = List;                  //第三种数据源赋值             CollectionVIEwSource collection = new CollectionVIEwSource { Source = List };             this.lbDetail.DataContext = collection;             this.spDetail.DataContext = collection;         }       

  本实例定义一个实体以及实体集合如下:

 

  public class Info             public string name { get; set; }              public string Author { get; set; }              public string Content { get; set; }              public string WriteDate { get; set; }              public static ObservableCollection<Info> GetList()         {             ObservableCollection<Info> List = new ObservableCollection<Info>();             List.Add(new Info() { name = "文章一", Author = "作者一", Content = "内容一", WriteDate = "2009-02-03" });             List.Add(new Info() { name = "文章二", Author = "作者二", Content = "内容二", WriteDate = "2009-03-03" });             List.Add(new Info() { name = "文章三", Author = "作者三", Content = "内容三", WriteDate = "2009-04-03" });             List.Add(new Info() { name = "文章四", Author = "作者四", Content = "内容四", WriteDate = "2009-05-03" });             List.Add(new Info() { name = "文章五", Author = "作者五", Content = "内容五", WriteDate = "2009-06-03" });             List.Add(new Info() { name = "文章六", Author = "作者六", Content = "内容六", WriteDate = "2009-07-03" });             return List;         }       

  本实例采用Vs2010+Silverlight 4编写,如需源码请点击 SLBinding3.rar 下载。

总结

以上是内存溢出为你收集整理的Silverlight实用窍门系列:58.Silverlight中的Binding使用(三)-数据集合绑定全部内容,希望文章能够帮你解决Silverlight实用窍门系列:58.Silverlight中的Binding使用(三)-数据集合绑定所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1069347.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存